473,545 Members | 2,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function to simplify data

Hi Folk

I seem to be writing the following a lot (this is just one of many
examples):

$sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
$query = mysql_query($sq l);
while($row = mysql_fetch_row ($query)) {
$ei = $row[0];
$en = $row[1];
$content .= '<li><a href="test.php? i='.$data("ID") .......
}

I want to write a function like this

function getdata($sql) {
$query = mysql_query($sq l);
while($row = mysql_fetch_row ($query)) {
$v[$i][{name of first column in select query (e.g. ID)}] = $row[0];
$v[$i][{name of first column in select query (e.g. NAM)}] = $row[1];
$i++;
}

Then, in my code I can write something like

$sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
$data = getdata($sql)
while(loop through array - i have no idea how you do this - but can look it
up) {
$content .= '<li><a href="test.php? i='.$data("ID") .......
}

What is the most efficient way to write such a function???

Any help appreciated.

- Nicolaas
Sep 3 '05 #1
5 1208
windandwaves wrote:
What is the most efficient way to write such a function???


function getdata($sql) {
$result = @mysql_query($s ql) or die("Error: " . mysql_error());
$ret = array();

while($row = mysql_fetch_ass oc($result)) {
$ret[] = $row;
}
mysql_free_resu lt($result);
return $ret;
}
$sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
$data = getdata($sql)

foreach ($data as $d) {
$content .= '<li><a href="test.php? i='.$d["ID"].......
}
JW

Sep 3 '05 #2
Janwillem Borleffs wrote:
windandwaves wrote:
What is the most efficient way to write such a function???


function getdata($sql) {
$result = @mysql_query($s ql) or die("Error: " . mysql_error());
$ret = array();

while($row = mysql_fetch_ass oc($result)) {
$ret[] = $row;
}
mysql_free_resu lt($result);
return $ret;
}
$sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
$data = getdata($sql)

foreach ($data as $d) {
$content .= '<li><a href="test.php? i='.$d["ID"].......
}

Thank you SO MUCH.... Awesome
Sep 3 '05 #3
windandwaves wrote:
Janwillem Borleffs wrote:
windandwaves wrote:
What is the most efficient way to write such a function???


function getdata($sql) {
$result = @mysql_query($s ql) or die("Error: " . mysql_error());
$ret = array();

while($row = mysql_fetch_ass oc($result)) {
$ret[] = $row;
}
mysql_free_resu lt($result);
return $ret;
}
$sql = 'SELECT `ID`, `NAM` FROM HEP ORDER BY `ID`';
$data = getdata($sql)

foreach ($data as $d) {
$content .= '<li><a href="test.php? i='.$d["ID"].......

Does it slow things down to use proper names rather than numbers, i.e.

$d["NAM"] rather than $d[0]

Thank you again

- Nicolaas
Sep 3 '05 #4
windandwaves wrote:
Does it slow things down to use proper names rather than numbers, i.e.

$d["NAM"] rather than $d[0]


No, but using named keys is more verbose then using array indexes. When
referencing a key "NAM" then you can be sure you get the value of the key
"NAM"; with an index this doesn't have to be the case.
JW

Sep 4 '05 #5
Janwillem Borleffs wrote:
windandwaves wrote:
Does it slow things down to use proper names rather than numbers, i.e.

$d["NAM"] rather than $d[0]


No, but using named keys is more verbose then using array indexes. When
referencing a key "NAM" then you can be sure you get the value of the key
"NAM"; with an index this doesn't have to be the case.


I'm also a big fan of hash arrays; I use them often for readability.
But, AFAIK, hash array lookup takes linear search and would be slower
in terms of speed. (But, I'm not sure about the implementation in PHP)

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Sep 4 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1659
by: beliavsky | last post by:
How can I pass global data to function stored in a separate file? For example, in file "funk.py" is the code ipow = 2 def xpow(xx): return xx**ipow and in "xfunk.py" is the code from funk import xpow,ipow xx = 4.0
58
10072
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
0
1533
by: Stylus Studio | last post by:
Stylus Studio 6 XML Enterprise Edition Now Integrates with TigerLogic XDMS XQuery and Native XML Database Bedford, MA, -- Stylus Studio ( http://www.stylusstudio.com ), the industry-leading provider of XML development tools for advanced XML data integration, and Raining Data Corporation (NASDAQ: RDTA), a leading provider of reliable data...
0
2292
by: Stylus Studio | last post by:
DataDirect XQuery(TM) is the First Embeddable Component for XQuery That is Modeled after the XQuery API for Java(TM) (XQJ) BEDFORD, Mass.--Sept. 20, 2005--DataDirect Technologies (http://www.datadirect.com), the software industry leader in standards-based components for connecting applications to data and an operating unit of Progress...
8
3652
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in that it copies one block of data to another block of data until the block that is being copied contains a zero/null. the difference with this code is...
2
3021
by: Tony Cox | last post by:
Hi. I'm new to VB.NET, so please be gentle ;-) I'm trying to transfer VB.NET structures to a shared memory area at a fixed location in process memory space. The structures themselves consist of simple data types (integers, shorts, etc.), and the data transfer to/from the shared memory area is handled with the Marshalling routines from...
8
1883
by: ecir.hana | last post by:
Dear list, maybe I'm overlooking something obvious or this is not possible at all or I don't know. Please, consider the following code: ## insert here anything you like def changer():
6
1880
by: Patrick | last post by:
Hi All, Kind of new to this. What I have below works, but I am wondering if there is a way to make it more efficient/simpler instead of having to write an if, tr, td, blah for each datatype. How would I simplify this? Any thoughts are appreciated. Thanks, Patrick
20
2201
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to think my way through what I want to do now. I don't know why - I'm fine with most other aspects of the language, but my brain goes numb when I'm...
0
7496
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7428
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7452
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6014
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.