CDubYaa
CDubYaa

Reputation: 45

want to display results in horizontal table using php mysql

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.

Upvotes: 0

Views: 2298

Answers (2)

Kristen Waite
Kristen Waite

Reputation: 1465

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

Upvotes: 1

jessier3
jessier3

Reputation: 849

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.

Upvotes: 0

Related Questions