MojtabaSh
MojtabaSh

Reputation: 637

How to convert string to variable for use in server code

I want convert $RowNumber from string to a variable, for use in while, do is that possible?!how to do it?

$row1 = mysqli_query($Database,"SELECT * FROM Table1 ORDER BY ID");
$row2 = mysqli_query($Database,"SELECT * FROM Table2 ORDER BY ID");
// Others Rows code

for ($num = 1; $num <= 6; $num++)
{
  $RowNumber = "row" . $num;   // Save RowNumber for use in while

  echo '<tr>';
  // when it's used it not work becuse it's just a string only and 
  // i want convert it to variable to use in while
  while($row = mysqli_fetch_array($RowNumber)) 
  {
    echo  '<td>
        <div class="Image_DIV" id="'; echo $row['DIV_ID'];  echo  '">
          <table>
            <tr><td class="Image"><img class="ImageOfDiv" 
              src="3DGallery/Chosen/Small/'; echo $row['src']; echo '"/></td></tr>
            <tr>
              <td class="ImageDescribe">'; echo $row['describe']; echo '</td></tr></table>
        </div>
        </td>';
   }
   echo '</tr><tr>';
}

Upvotes: 0

Views: 85

Answers (3)

AeroX
AeroX

Reputation: 3453

If Table1 and Table2 have the same columns then you should UNION them together in your SQL then work on a single results set.

$results = mysqli_query($Database, "SELECT ID, DIV_ID, src, describe FROM `Table1` UNION SELECT ID, DIV_ID, src, describe FROM `Table2` ORDER BY ID");

echo '<tr>';
$x = 0;
while($row = mysqli_fetch_array($results))
{
    echo '<td>';
    echo $row['DIV_ID'];
    echo $row['src'];
    echo $row['describe'];
    echo '</td>';
    if( ++$x % 3 == 0 ) {
        echo '</tr><tr>';
    }
}
echo '</tr>';

Upvotes: 0

Philip G
Philip G

Reputation: 4104

Try this:

$rows[] = mysqli_query($Database,"SELECT * FROM Table1 ORDER BY ID");
$rows[] =  mysqli_query($Database,"SELECT * FROM Table2 ORDER BY ID");
// Others Rows code

foreach ($rows as $current)
{

  echo '<tr>';
  // loop the query
  while($row = mysqli_fetch_array($current)) 
  {
    echo  '<td>
        <div class="Image_DIV" id="'; echo $row['DIV_ID'];  echo  '">
          <table>
            <tr><td class="Image"><img class="ImageOfDiv" 
              src="3DGallery/Chosen/Small/'; echo $row['src']; echo '"/></td></tr>
            <tr>
              <td class="ImageDescribe">'; echo $row['describe']; echo '</td></tr></table>
        </div>
        </td>';
   }
   echo '</tr><tr>';
}

You cant assign variables like that without using php Eval: http://www.php.net/eval, but that is realy bad practise!

or using ${$var} syntax. But arrays is probably easier to work with.

Upvotes: 0

Nouphal.M
Nouphal.M

Reputation: 6344

Try ${$RowNumber} instead of $RowNumber for your purpose, If you need more info you can find it here. But as Mr. raina77ow has commented arrays are a good option.

Upvotes: 1

Related Questions