Variable loses its value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

Variable loses its value

Post by AYYASH »

Code: Select all

for ($j=0; $j < $rows; $j++) {
    for ($i=0; $i < 6; $i++) {
  $row = mysql_fetch_array($query);
  $array[$j][$i] = $row['thumb'];
  $project[$j][$i] = $row['id'];
    }
  }
This code works fine in my pc where I have php5 with apache 1.3.
When I uplaod it to the site and click the link that contains $project[$j][$i], it loses its value. When I print out its value only ($row['id']) it shows up.

see it here
http://www.mohtaraf.com/portfolio/photo ... 10&img=101
click on one of the thums in the page and you'll that "project" will be= ''. Click on it again, it will get back the value. Doesn't make sence to me.
The other thing that make me out of my mind is that $array[$j][$i] is working fine with no probelms at all.

Any idea why?
pennythetuff
Forum Newbie
Posts: 22
Joined: Sun Feb 19, 2006 6:05 pm
Location: Kokomo, Indiana

Post by pennythetuff »

Could you post more code please?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Usually you iterate over query results like this

Code: Select all

while($curr_row=mysql_fetch_assoc($result)) {
   echo $row['id'];
   echo $row['whatever'];
   /* or
   foreach($row as $field_name => $field_value) {
   } */
}
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

Post by AYYASH »

this is the part of the code that comes before the one I posted

Code: Select all

$select = "SELECT * FROM projects WHERE category='".$_REQUEST['category']."'";
$query = mysql_query($select);
$nums = mysql_num_rows($query);
// define number of thumbs rows
if ($nums <= 6) {
$rows = "1";
} elseif ($nums <= 12) {
$rows = "2";
} elseif ($nums <= 18) {
$rows = "3";
} elseif ($nums <= 24) {
$rows = "5";
} elseif ($nums <= 30) {
$rows = "6";
} else {
$rows = "7";
}
for ($j=0; $j < $rows; $j++) {
for ($i=0; $i < 6; $i++) {
$row = mysql_fetch_array($query);
$array[$j][$i] = $row['thumb'];
$project[$j][$i] = $row['id'];
}
}
The next code is the part inside the HTML page:

Code: Select all

<table border="0" cellpadding="0" cellspacing="4" bgcolor="4E4E36">
<?php for ($j=0; $j < $rows; $j++) { ?>
<tr>
<?php for ($i=0; $i < 6; $i++) { ?>
<?php if ($array[$j][$i]=='') { print ""; } else { ?>
<td valign="middle"><a href="photo.php?album=by_category&category=<?php print $_REQUEST['category']; ?>&project=<?php print $project[$j][$i]; ?>&img=<?php print $project[$j][$i]."1"; ?>"><img border="0" src="<?php print $array[$j][$i]; ?>" /></a></td>		
<?php } ?>
<?php } //if ?>
<?php } //$i ?>
</tr>
<?php } //$j ?>
</table>
Post Reply