0

I am using the following code to get results and want to have it displayed in two rows horizontially.

echo "<table border='0' width='700px' align='center'>";
echo "<tr>";
while($number = mysqli_fetch_array($result2))
{
    echo "<td class='ball_p'>" . $number['number'] . "</td>";

    **echo "</tr><tr>";**

    echo "<td class='search'>" . $number['count'] . "</td>";
}
echo "<tr>";
echo "</table>";

Hence I need this part of the code not to be included in the loop echo "";

Please help, this must be really simple.

I want the results to be displayed 10 across as I limit my query to the top 10.

So it would be:

Number Number Number etc. Count Count Count etc.

2
  • Can you explain "two rows horizontally"? How many cells horizontally? Also, your second echo "<tr>"; should be echo "</tr>";. Commented Nov 27, 2013 at 20:09
  • I am trying to display 10 items across in two row. In my query I Limit 10. Commented Nov 27, 2013 at 20:59

2 Answers 2

1

Because the tables need to be drawn one row at a time, you could store all of your number values in one array and all of your count values in a second array, then loop through each array as you're building your rows. So first get your values from the database:

// let's get a variable with the total number of records, i.e. horizontal cells:
$totalRecords = 0;

// get values from db:
while($number = mysqli_fetch_array($result2))
{
    $numberArr[] = $number['number'];
    $countArr[] = $number['count'];
    $totalRecords++;
}

Then in another set of loops render the table (one loop for each row):

// first row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i) // you could also use count($numberArr) instead of $totalRecords
{
    echo "<td class='ball_p'>" . $numberArr[$i] . "</td>";
}
echo "</tr>";

// second row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i) 
{
    echo "<td class='ball_p'>" . $countArr[$i] . "</td>";
}
echo "</tr>";
Sign up to request clarification or add additional context in comments.

Comments

0

What about this:

$i = 0;
while($number = mysqli_fetch_array($result2))
{
   echo "<td class='ball_p'>" . $number['number'] . "</td>";
    while($i++ >= 1)
    {
      echo "</tr><tr>";
      $i = 0;
    }
    echo "<td class='search'>" . $number['count'] . "</td>";
}

I haven't tested it but the ++ after the $i in the second while loop should increment it after the evaluation which should skip the </tr><tr> the first time and print it the second.

1 Comment

This does not produced the desired results. But I do appreciate your time.

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.