0

Hey Everyone i got a little problem! I know its something simple but for some reason i can not figure it out (arrays are kicking my butt!)

I want the results to display like the following...

title 1 | title 2 | title 3 | title 4 |
Title 5 | title 6 | title 7 | title 8 |

But for some reason when the code runs it shows as the following...

t | i |

It spells out the title 1 and not the whole word for each cell of the table. What am i doing wrong?

$result = mysqli_query($con,"SELECT title FROM donuts"); 
$rows = 2; 
$cols = ceil(count($result)/$rows); 
$row = mysqli_fetch_array($result); 
echo $result=$row['title']; 
echo "<table border='1'>"; 
$i=0; 
for($tr=1;$tr<=$rows;$tr++) {
   echo "<tr>"; 
   for($td=1;$td<=$cols;$td++) {  
       if (isset($result[$i])) {
           echo "<td>".$result[$i]."</td>"; $i++;
       } 
   }
   echo "</tr>";
} 
echo "</table>"; 

Note the table has no limit on how many columns there are just rows.

Link to working example http://lakeside.donavonscreativeinnovations.com/

2
  • try printing out the value of $row. What does your echo $result look like? It looks like you are iterating over the array of characters within a SINGLE element from your result set. Commented Jan 28, 2014 at 15:59
  • you are echoing $result[$i], which is the i-th letter of $result string (the string is understood as a char array, like in good old C language) Commented Jan 28, 2014 at 16:00

3 Answers 3

2

You have to loop through the results. Something like this:

// $numrows = mysqli_num_rows($result); // Count rows if you want to know

while( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) { // Loop through rows
    echo "<tr>";
    foreach($row as $key => $value) { // Loop through columns
        echo "<td>" . $value . "</td>";
    }
    echo "</tr>";
}

At the moment you loop through each character of a column.

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

4 Comments

Wouldn't that duplicate the columns -- it defaults to MYSQLI_BOTH.
Thank you for your help although... you code displays as the following title 1 | title 1 ----------------- title 2 | title 2 ----------------- title 3 | title 3 ----------------- should be title 1 | title 2 | title 3 ----------------- title 4 | title 5 | title 6 how do i fix this?
I have added MYSQLI_ASSOC to make it only get columns once.
Niels , forgive me for so many questions it looks like they are now displaying the results in a single line vertically . Simple fix?
0

You might want to try it like this:

$result = mysqli_query($con, "SELECT title FROM donuts");

echo '<table>';

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{

  echo '<tr>';

  foreach ($row as $cell)
  {
    echo '<td>' . $cell . '</td>';
  }

  echo '</tr>';

}

echo '</table>';

Comments

0

Here is your problem:

echo $result=$row['title']; 

and then you loop through this with the code:

$i=0; for($tr=1;$tr<=$rows;$tr++)
              { echo "<tr>"; 
                    for($td=1;$td<=$cols;$td++)
                       {  
                   if (isset($result[$i])) {
                     **echo "<td>".$result[$i]."</td>"; $i++;**

Thus you loop through a string outputting each character in that string.

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.