0

EDIT: This is what I am trying to achieve: https://i.sstatic.net/QJD2Z.png

I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.

Here is my current code:

include('connect.db.php'); 
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
  // display records if there are records to display
  if ($result->num_rows > 0)
  { 
    // display records in a table
    echo "<table width='415' cellpadding='0' cellspacing='0'>";
    // set table headers
    echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
        <td><img src='media/title_status.png' alt='Status'/></td>
      </tr>";
    echo "<tr>
        <td><div class='tpush'></div></td>
        <td>&nbsp;</td>
      </tr>"
    while ($row = $result->fetch_object())
    {
      echo "<tr>";
      echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
      echo "<td>" . $row->priority . "</td>";
      echo "</tr>";
    }
    echo "</table>";
  }
  // if there are no records in the database, display an alert message
  else
  {
    echo "No results to display!";
  }
}
// show an error if there is an issue with the database query
else
{
   echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
3
  • First off, take the time to shift indents in your posted code. There should be no horizontal scrollbars... Commented Apr 17, 2012 at 1:53
  • You told us what you're trying to do and you showed us your attempt. Good so far - but what's the actual problem you're having at the moment? Commented Apr 17, 2012 at 1:54
  • This is what I am trying to achieve: i.imgur.com/KE9xx.png Commented Apr 17, 2012 at 1:58

3 Answers 3

1

A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:

$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);

$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;

// Put results in an array
while($r = mysql_fetch_assoc($query)) {
    $rows[] = $r;
    $i++;
}

//display results in a table of 2 columns

echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
Sign up to request clarification or add additional context in comments.

Comments

0
<table>
  <tr>
    <td>ProjectName</td>
    <td>Status</td>
    <td>ProjectName</td>
    <td>Status</td>
  </tr>
  <?php
     while($row = $result->fetch_object()) {
        echo "<tr>";
        echo "<td>".$row->ProjectName."</td>";
        echo "<td>".$row->Status."</td>";
        echo "<td>".$row->ProjectName."</td>";
        echo "<td>".$row->Status."</td>";      
        echo "</tr>";
     }
  ?>
</table>

This is the thing on picture. With a bit CSS you can manipulate the tds.

Comments

0

Your function should look similar to this:

$query = "SELECT *
         FROM todo 
         ORDER BY id";
$result = $mysqli->query($query);

while($row = $result -> fetch_array()) {
   $feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;

Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.

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.