0

I want to show the array, after array[0] has shown, show next data , loop to do that before have shown all data in database. e.g:

Array ( 
  [0] => Array ( 
    [ID] => 1 [name] => peter [time] = >1:00
  )
  [1] => Array ( 
    [ID] =>2 [name]=> merry [time]=>3:00
  )
  ...etc

I wish that:

-------------- |id|name |time|
-------------- |1 |peter|1:00|
-------------- |2 |merry|3:00|
-------------- |..|.....|.:..|

but the real is just only show array[0] . like that:

 -------------- |id|name |time|
 -------------- |1 |peter|1:00|
 ---------------

Can you tell me how to improve that and correct that?

 $sql = "SELECT * FROM mytable";
 $result = mysqli_query($conn, $sql);
 $datas = array();

if (mysqli_num_rows($result) > 0)
{
  while($row = mysqli_fetch_assoc($result)){
    $datas[] = $row; 
  }
}

$result = $conn->query($sql);
$x = 0;               
$a = $datas[$x];

if($a = array())
{
  echo "";
}
else
{
  foreach($datas[$x] as $data){
    echo $data."<td>"."<br>";
    $x++; 
  }
}
2
  • if($a = array()) ?? = assign a value == test a variable for equality Commented Sep 7, 2020 at 9:35
  • Why are you rerunning the query $result = $conn->query($sql); after your first IF, you never make use of the reault of that query and it has aleady been run and saved into an array?? Commented Sep 7, 2020 at 9:38

1 Answer 1

3

There are quite a few issues in your code:

  1. $result = $conn->query($sql) is redundant - you already ran exactly the same query a few lines earlier.

  2. if($a = array()) makes no sense. I think you're probably trying to test whether $a is definitely an array? But what you're really doing is assigning a new empty array to it. It's irrelevant anyway because you never use $a for anything, and we know that $datas will always be an array, so there's no need to check if it's an array. If there are no rows, the loop just won't run.

  3. Writing "<td>"."<br>" will generate invalid HTML. Either use tables and rows, or just use line breaks. You need to make up your mind. Tables is probably better for presenting tabular data...

  4. Most importantly for your question, $datas[$x] only represents the first array of data in $datas. Your loop only loops over the items in that first array. You need to loop over all the indexes in $datas.

Here's a fixed version of your code:

$sql = "SELECT * FROM mytable";
$result = $conn->query($sql);
$datas = array();

if (mysqli_num_rows($result) > 0)
{
  while($row = mysqli_fetch_assoc($result)){
    $datas[] = $row; 
  }
}

echo "<table>"; //start a table
echo "<tr><th>ID</th><th>Name</th><th>Time</th></tr>"; //print the table header

foreach($datas as $row) //loop over all the rows
{
  echo "<tr>"; //start a new row

  foreach ($row as $field) //loop over the fields in each row.
  {
    echo "<td>".$field."</td>"; //print a single field in a table cell
  }
  echo "</tr>"; //end the row
}
echo "</table>"; //end the table
Sign up to request clarification or add additional context in comments.

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.