Jump to content

[SOLVED] Is it possible to loop through a set of variables that have been set?


Solarpitch

Recommended Posts

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?

Link to comment
Share on other sites

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
  }

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>";

        }

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>";

}
?>

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.