0

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>';
}
4
  • 2
    That's not how you use mysqli_fetch_array() you need to feed it the returned object from mysqli_query() Commented Dec 18, 2013 at 17:21
  • Many Thanks Nouphal.M, It's true. i wnat reduce my codes with this command Commented Dec 18, 2013 at 17:24
  • Better solution would be to create array with all rows: $rows[] = mysqli_query.... foreach($rows as $row) { Commented Dec 18, 2013 at 17:26
  • Thanks raina77ow and Mahakala Array's good way for create loops Commented Dec 18, 2013 at 17:34

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

Ok, I can use it, but i need add a <tr> tag after every 3 rows, and to do it, i think i can just creat a for or foreach while, thanks
@user3110164 Edited answer to split output to a new table row after every 3 results as requested.
Oh my god, it's true, but just need ($x += 1) and (x % 3 == 0) and i think you forgot it, many thanks mr. AeroX
@user3110164 Yes I realised I made a mistake and have corrected it in the answer. It should have been if( ++$x % 3 == 0 )
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.