Simplify my Variable Assignment

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
yi3artist
Forum Newbie
Posts: 4
Joined: Sun Jul 06, 2003 11:30 am

Simplify my Variable Assignment

Post 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;
User avatar
Unifex
Forum Newbie
Posts: 14
Joined: Mon Jul 07, 2003 12:29 am

Post 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...
yi3artist
Forum Newbie
Posts: 4
Joined: Sun Jul 06, 2003 11:30 am

You ... are a God!

Post by yi3artist »

Thank You, it works great!
User avatar
Unifex
Forum Newbie
Posts: 14
Joined: Mon Jul 07, 2003 12:29 am

Post by Unifex »

You're welcome :)
Post Reply