Jump to content


Photo

naming PHP variables according to a MySQL array index value


  • Please log in to reply
6 replies to this topic

#1 baran

baran
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 September 2003 - 06:11 PM

I have this code to get the fields from a table for as long as there are rows in the table:
[php:1:5f84876c54]<?php
$SQL = \" SELECT * FROM $tablename \";

# execute SQL statement
$retid = mysql_query($SQL, $cid);

# check for errors
if (!$retid) { echo(\"ERROR: \" . mysql_error() . \"n$SQLn\"); }
else {


# fill variables
while ($row = mysql_fetch_array($retid)) {
$catname = $row[\"catname\"];
$dogname = $row[\"dogname\"];
$pigname = $row[\"pigname\"];
$horsename = $row[\"horsename\"];

?>[/php:1:5f84876c54]

What I want to do is create and fill up different PHP variables for each row of my table. so I want to have a variable called $catname1 for the cat\'s name in the first row, but also to have a variable called $catname2 for the cat\'s name in the second row, and $catname3, $catname4 etc etc until there\'s no more rows.

Any help greatly appreciated, I have vague ideas about using mysql_fetch_row() to get the row number and append this onto the variable name but am not really sure how.

#2 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 15 September 2003 - 05:23 AM

Why dont you use an array in this case?

while ($row = mysql_fetch_array($retid)) {         $catname[] = $row["catname"];         $dogname[] = $row["dogname"];         $pigname[] = $row["pigname"];         $horsename[] = $row["horsename"]; }

This will strore the values in an array

To loop thru you can use this

for($i=0;$i<=count($catname);$i++){echo $catname[$i];}

Hope this helps!
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#3 baran

baran
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 15 September 2003 - 07:50 AM

shivabharat,

Great stuff!. Many thanks.

I suppose that was quite an easy one!

#4 baran

baran
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 15 September 2003 - 07:25 PM

actually I wrote my last post before I\'d had a chance to test it. It doesn\'t work! :oops:

I\'ve had a good read of
http://www.php.net/m...types.array.php
but am a bit unsure of how to declare an array properly using the square brackets and filling the array with data from MySQL using the mysql_fetch_array function.

does anyone have any ideas whats wrong with the last bit of code posted. I\'m sure my table is fine.

#5 baran

baran
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 15 September 2003 - 07:35 PM

actually this works:
[php:1:747c443ec9]<?php
while ($row = mysql_fetch_array($retid)) {
$catname[] = $row[\"catname\"];
$dogname[] = $row[\"dogname\"];
$pigname[] = $row[\"pigname\"];
$horsename[] = $row[\"horsename\"];
}

echo $catname[1];
echo $catname[2];

?>[/php:1:747c443ec9]

and thats really all I need. Thanks.

#6 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 16 September 2003 - 04:57 AM

Hi baran,

Can you let me know what was the issue you faced when you used that code? This will help me in correcting the code so that it can help others.
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#7 baran

baran
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 16 September 2003 - 09:48 AM

shivabharat,

didn\'t echo anything. I placed the for loop outside of the while loop. from what little experience of PHP I have your code looked fairly foolproof so maybe something was wrong my end. anyway thanks for your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users