...

View Full Version : having problems echoing $Row = mysql_fetch_array($Result); with a for loop



DiarYofaMadmaN
05-08-2004, 09:36 PM
<?php
$link = mysql_connect($Host, $User, $Password);
mysql_select_db($DBName, $link);

$Query = "SELECT * FROM Categories";
$Result = mysql_query($Query);
$num_rows = mysql_num_rows($Result);
$Row = mysql_fetch_array($Result);

if ($_GET['CatID'] == Company) {
$col = "Company_title";
$nav_title = "images/company-title.jpg";
}
if ($_GET['CatID'] == Hardware) {
$col = "Category";
$nav_title = "images/hardware-title.jpg";
}
?>


<?php
echo '<ul>';
for ($i = 1; $i <= $num_rows-1; $i++) {
echo '<li>'.$Row[$col].'</li>';
}
mysql_close ($Link);
echo '</ul>';
?>

Well, I'm having a hard time printing out the $Results using the for loop. I have no clue where to beginning. $col == Category which is the Column name. This works using a while loop... Any help would be greatly appreciated.

Edit:
It only prints out the first row which is Cases...

-peace

DiarYofaMadmaN
05-09-2004, 12:16 AM
Ahh nvm I figured it out, I had to put $Row = mysql_fetch_array($Result); inside the loop.

-peace

missing-score
05-09-2004, 02:14 AM
However, there is a better way to do this:



while($row = mysql_fetch_assoc($Result)){
echo '<li>'.$row[$col].'</li>';
}


This is the best way to do this, and it cuts out the need for mysql_num_rows(). Also note i am using mysql_fetch_assoc(), not fetch_array(), as assoc only returns the associative array (array with the names)

DiarYofaMadmaN
05-09-2004, 03:26 AM
However, there is a better way to do this:



while($row = mysql_fetch_assoc($Result)){
echo '<li>'.$row[$col].'</li>';
}


This is the best way to do this, and it cuts out the need for mysql_num_rows(). Also note i am using mysql_fetch_assoc(), not fetch_array(), as assoc only returns the associative array (array with the names)


Hey! Thanks for that tip, I really need to learn these different mysql_ functions better.

One more question, is it possible to get the last value in mysql_fetch_assoc($Result)?

-Rich

missing-score
05-09-2004, 04:04 AM
how do you mean exactly? do you mean get values of the last row returned?

DiarYofaMadmaN
05-09-2004, 04:19 AM
how do you mean exactly? do you mean get values of the last row returned?

Firstly, the reason why I used a for loop was because(get ready this may be kinda confusing for me to explain). Anyways, The reason why I used a for loop is because the way I have my visual design of my page. I would have used CSS but I don't know it to well so I used a table with three cells. I spliced up my image into three images. The first for the title, second for the back of the unordered list, and the third image for the bottom. For example of what I'm talking about go to: http://www.maxforcepc.com/itemlist.php?CatID=2 If you look closely to the last item in my list it says "Troubleshooting
".

Troubleshooting should be echoed out into the last cell(In that big gap in the bottom) to look uniformed. So, the for loop loops in this case from 1 through 2 and prints out Networking and Upgrades. Then in the third cell I need to echo out the last result in the mysql array. With the while loop it's printing through the full list. I do relize I can edit my sql query but I can't do it that way because I'm changing the navigation look with the url.

-Rich

Mhtml
05-09-2004, 09:09 AM
You could have used mysql_result as well, which is how I do it if I need to use a for loop..



for( $i = 0; $i < mysql_num_rows( $result ); $i++ ){
echo mysql_result( $result, $i, 'field' );
}

moneybux
03-13-2008, 02:52 AM
take a look at this:

this is causing errors on my site at http://www.moneybux.biz. can someone tell me a solution?:
$row = mysql_fetch_array($result);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum