Page 1 of 1

Simplify my Variable Assignment

Posted: Sun Jul 06, 2003 8:53 pm
by yi3artist
I have some code that I will need to put on multiple pages, but the length of code is very large (the following is a snipet of the basic pattern):

Code: Select all

$giant_katana = mysql_result ($result, 0 , giant_katana);
$claymore = mysql_result ($result, 0 , claymore);
$great_axe = mysql_result ($result, 0 , great_axe);
$scourge = mysql_result ($result, 0 , scourge);
How can I simplify this code? Is there some loop function that can do this? Note: this is a minor section of the patterned code, so it will be useful for me to have this simplified.

Though the next code segment has no mySQL it also repeats. If it could be stuck in a loop statement or something, that would be awesome:

Code: Select all

if ($giant_katana)
{ print "
<tr>
   <td><li><font color=gray>&#1111;$giant_katana] giant katana</font></td>
</tr>
"; &#125;

if ($claymore)
&#123; print "
<tr>
   <td><li><font color=gray>&#1111;$claymore] claymore</font></td>
</tr>
"; &#125;

if ($great_axe)
&#123; print "
<tr>
   <td><li><font color=gray>&#1111;$great_axe] great axe</font></td>
</tr>
"; &#125;

if ($scourge)
&#123; print "
<tr>
   <td><li><font color=gray>&#1111;$scourge] scourge</font></td>
</tr>
"; &#125;

Posted: Mon Jul 07, 2003 12:29 am
by Unifex
This should work...

Code: Select all

<?php
$items = array( "giant_katana", "claymore", "great_axe", "scourge");

foreach ($items as $item) {
  $$item = mysql_result ($result, 0 , $item);
}

?>
Variable variables are very cool things if used right :)

You can use the same thing for the second part of the question also...

You ... are a God!

Posted: Mon Jul 07, 2003 9:05 am
by yi3artist
Thank You, it works great!

Posted: Mon Jul 07, 2003 5:54 pm
by Unifex
You're welcome :)