Solarpitch Posted November 7, 2007 Share Posted November 7, 2007 Hey, Just want to know if its possible to loop through a set of variables that have been set. Say for example I have 5 image variables: $image1; $image2; $image3; $image4; $image5; And for each variable I have... $image1 = $row[8]; if($image1 != "") { $image1 = "<a href='link.com'</a>"; } Then I want to be able to loop and display the links only when the $image variable is != ""; Or does this even make sense? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 7, 2007 Share Posted November 7, 2007 Well the easiest thing to do is use an array next time, then you can just use a foreach loop. However, if you don't want to change code, you can take advantage of PHP's { } variable syntax (I don't know what the official name is). Let's say you had a variable named 'Hello'. The next two lines of code are equivalent: $Hello = 'Tom'; ${'Hello'} = 'Tom'; Basically, if you have the following sequence: dollar sign, open curly bracket, any PHP expression, and then a closed curly bracket, the PHP expression becomes the variable name. So in your case: for($i = 1; $i <= 8; $i++){ echo '<pre>' . print_r(${'image' . $i}, true) . '</pre>'; // echo's image1 through image8 // To avoid a bunch of funny syntax in your code, I'd set a temp variable and use that $tmp = ${'image' . $i}; // Use $tmp from this point forward } Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 Thanks for that! I'll have a go at integrating it now Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 Flawless! Thanks a mill Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 7, 2007 Share Posted November 7, 2007 I just noticed in your OP you said you had 5 variables but I wrote a loop for 8; just make sure you didn't copy my code verbatim and include my mistake. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 I noticed that the way you have it echo'd there it seems to print the link sat the top of the page, even if I specify this.. echo "<td><a href='property.php?property_id=".$row[0]."'><img src='designs/house2.png' border='0'></img>".$tmp."</td>"; where I have temp listed above it doesnt seem to print out there... rather, it prints where you have echo'd the loop. Is there any reason for this? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 7, 2007 Share Posted November 7, 2007 Paste your code. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 Here's the loop it runs in... while(($row = mysql_fetch_row($result)) != false) { $location = $row[2]; $location = getlocationname($location); $type = $row[3]; $type = gettypename($type); $image1 = $row[8]; if($image1 != "") { $image1 = "<a href='admin/". $row[8] . "' rel='lightbox[".$row[0]."]' title='".$row[21]."'><img src='designs/camera2.png' border=0></img></a>"; } $image2 = $row[9]; if($image2 != "") { $image2 = "<a href='admin/". $row[9] . "' rel='lightbox[".$row[0]."]' title='".$row[22]."'></a>"; } $image3 = $row[10]; if($image3 != "") { $image3 = "<a href='admin/". $row[10] . "' rel='lightbox[".$row[0]."]' title='".$row[23]."'></a>"; } $image4 = $row[11]; if($image4 != "") { $image4 = "<a href='admin/". $row[11] . "' rel='lightbox[".$row[0]."]' title='".$row[24]."'></a>"; } $image5 = $row[12]; if($image5 != "") { $image5 = "<a href='admin/". $row[12] . "' rel='lightbox[".$row[0]."]' title='".$row[25]."'></a>"; } $image6 = $row[13]; if($image6 != "") { $image6 = "<a href='admin/". $row[13] . "' rel='lightbox[".$row[0]."]' title='".$row[26]."'></a>"; } for($i = 1; $i <= 6; $i++){ echo print_r(${'image' . $i}, true); $tmp = ${'image' . $i}; } echo "<table id='resultsrow' width='575' height='155' border='0' cellpadding='0' cellspacing='0'>"; echo "<tr>"; echo "<td rowspan='3' width='124' style='padding-left:15px;' align='center'><img src='admin/". $row[14] . "' border=1></img></td>"; echo "<td align='left' width='105' style='padding-left:12px; color:#719DC0;'><b>". $type ."</b></td>"; echo "<td align='left' width='200' style='padding-left:12px; color:#0A1871;'><b>". $row[27] . "</b></td>"; echo "<td align='left' width='70' style='padding-left:12px;'><b>€". number_format($row[4]) . "</b></td>"; echo "</tr>"; echo "<tr>"; echo "<td colspan='3' align='left' valign='top' style='padding-left:12px; padding-right:12px; padding-top:7px;'>". substr($row[5], 0, 150) . '...' . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td colspan='3' align='right' valign='bottom' width='70' style='padding-right:12px;'><a href='property.php?property_id=".$row[0]."'><img src='designs/house2.png' border='0'></img>".$tmp."</td>"; echo "</tr>"; } Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 7, 2007 Share Posted November 7, 2007 I assume you want this stuff to echo down where all the table stuff is. You have to look at what the code is doing and think about it. Here is the loop: for($i = 1; $i <= 6; $i++){ echo print_r(${'image' . $i}, true); $tmp = ${'image' . $i}; } I put that echo print_r() in there just to show you that it works and accesses your variables. There is a very good chance you do not want to keep it. Second, the loop sets a variable $tmp but does nothing with it, so essentially you will set $tmp 6 times and then after the loop $tmp is equal to the final value. So after the loop executes, we've printed information we didn't need / want and $tmp is only equal to the last value set. Then your code prints the table stuff. Do you see what's wrong with that picture? Here is a fixed version: echo "<table id='resultsrow' width='575' height='155' border='0' cellpadding='0' cellspacing='0'>"; echo "<tr>"; echo "<td rowspan='3' width='124' style='padding-left:15px;' align='center'><img src='admin/". $row[14] . "' border=1></img></td>"; echo "<td align='left' width='105' style='padding-left:12px; color:#719DC0;'><b>". $type ."</b></td>"; echo "<td align='left' width='200' style='padding-left:12px; color:#0A1871;'><b>". $row[27] . "</b></td>"; echo "<td align='left' width='70' style='padding-left:12px;'><b>€". number_format($row[4]) . "</b></td>"; echo "</tr>"; echo "<tr>"; echo "<td colspan='3' align='left' valign='top' style='padding-left:12px; padding-right:12px; padding-top:7px;'>". substr($row[5], 0, 150) . '...' . "</td>"; echo "</tr>"; echo "<tr>"; for($i = 1; $i <= 6; $i++){ $tmp = ${'image' . $i}; echo "<td colspan='3' align='right' valign='bottom' width='70' style='padding-right:12px;'><a href='property.php?property_id=".$row[0]."'><img src='designs/house2.png' border='0'></img>".$tmp."</td>"; } echo "</tr>"; Do you see now how the loop has been moved to where you intended to use the values? Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 Ahhh I see what you mean now! We need to loop through $temp where the result will be displayed rather than setting the value elsewhere and calling $temp as this will hold the last value in that set. Thanks again for your help! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 7, 2007 Share Posted November 7, 2007 I'd also recommend using mysql_fetch_assoc() and using associative arrays when dealing with database columns. 6 months from now, trying to remember what $row[0], $row[5], etc. mean will be difficult. And should you alter the table, the indexes may change. That would be a maintenance nightmare. (edit) Also, never blindly copy and paste code from the web, forums, etc. into your program. Always try and see what the other person did and then apply the general solution to your problem, not the specific code solution they provided. This serves many purposes. The first is that it enhances your capabilities and understanding of what's occurring. The second is that it protects you from other peoples' mistakes; you never know when they have a typo or logic error in their code that does something drastic (such as deleting the wrong files). As always, and I learned this in physics, after you come up with any answer you must always ask yourself the most important question: Does this make sense? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 7, 2007 Share Posted November 7, 2007 Just for fun, here is how you can eliminate most of your code in favor of a single loop: <?php while(($row = mysql_fetch_row($result)) != false){ $location = $row[2]; $location = getlocationname($location); $type = $row[3]; $type = gettypename($type); for($i = 1; $i <= 6; $i++){ $rowAdmin = $row[$i + 7]; $rowTitle = $row[$i + 20]; ${'image' . $i} = $rowAdmin; if($rowAdmin != ''){ ${'image' . $i} = "<a href=\"admin/{$rowAdmin}\" " . "rel=\"lightbox[{$row[0]}]\" " . "title=\"{$rowTitle}\">"; if($i == 1){ // I'm not sure why you only set this for one image ${'image' . $i} .= "<img src='designs/camera2.png' border=0>"; } ${'image' . $i} .= '</a>'; } } echo "<table id='resultsrow' width='575' height='155' border='0' cellpadding='0' cellspacing='0'>"; echo "<tr>"; echo "<td rowspan='3' width='124' style='padding-left:15px;' align='center'><img src='admin/". $row[14] . "' border=1></img></td>"; echo "<td align='left' width='105' style='padding-left:12px; color:#719DC0;'><b>". $type ."</b></td>"; echo "<td align='left' width='200' style='padding-left:12px; color:#0A1871;'><b>". $row[27] . "</b></td>"; echo "<td align='left' width='70' style='padding-left:12px;'><b>€". number_format($row[4]) . "</b></td>"; echo "</tr>"; echo "<tr>"; echo "<td colspan='3' align='left' valign='top' style='padding-left:12px; padding-right:12px; padding-top:7px;'>". substr($row[5], 0, 150) . '...' . "</td>"; echo "</tr>"; echo "<tr>"; for($i = 1; $i <= 6; $i++){ $tmp = ${'image' . $i}; echo "<td colspan='3' align='right' valign='bottom' width='70' style='padding-right:12px;'><a href='property.php?property_id=".$row[0]."'><img src='designs/house2.png' border='0'></img>".$tmp."</td>"; } echo "</tr>"; } ?> Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 Cheers man! You can stay here and tutor me for the night if ya like! Handy few tip youve given me Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 Oh, the only problem with the below code is that it prints out the "house2.png" image also. So I think the loops needs to skip this somehow. for($i = 1; $i <= 6; $i++){ $tmp = ${'image' . $i}; echo "<td colspan='3' align='right' valign='bottom' width='70' style='padding-right:12px;'><a href='property.php?property_id=".$row[0]."'><img src='designs/house2.png' border='0'></img>".$tmp."</td>"; } echo "</tr>"; Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 7, 2007 Share Posted November 7, 2007 Then just remove the html for that image. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 I am sorry.. but I am not sure what you mean but that? If I remove the HTML.. surely it wont position correctly in the table? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 7, 2007 Share Posted November 7, 2007 Oh, the only problem with the below code is that it prints out the "house2.png" image also. Look at the line of code in question: echo "<td colspan='3' align='right' valign='bottom' width='70' style='padding-right:12px;'><a href='property.php?property_id=".$row[0]."'><img src='designs/house2.png' border='0'></img>".$tmp."</td>"; Notice this in the code: <img src='designs/house2.png' border='0'></img> That is what is printing the house2.png. If you don't want something to be printed, remove it from the print (or echo) statement. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 You see I know but I want the looped code to append to the end of that link of code rather than having that line being looped. Is there no way to start the loop at the end of that html line is where $tmp starts. ...</td> (Loop out value of $temp) ...</td> <a href=''.....</a> <a href=''.....</a> <a href=''.....</a> <a href=''.....</a> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 7, 2007 Share Posted November 7, 2007 Do you mean like this: echo "<td colspan='3' align='right' valign='bottom' width='70' style='padding-right:12px;'><a href='property.php?property_id=".$row[0]."'><img src='designs/house2.png' border='0'></img>"; for($i = 1; $i <= 6; $i++){ echo ${'image' . $i}; } echo "</td>"; echo "</tr>"; All you have to do is restructure the code until it does what you want. If you don't want something printed multiple times, don't stick it in a loop, put it before or after the loop. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.