0

I am new to web development and starting learning about CRUD.

My question is that while I successfully show the table listing 3 product on 1 row, that on the second row the product no 4 are missing and skipping to the product no 5 and keep missing every last 3 row.

function getData(){
    global $connect;
    $query = "SELECT id, name, category, price, image, info FROM product_data";
    $results = mysqli_query($connect, $query) or die(mysql_error());
    echo "<table border = 0 >";
    $x=1;
    echo "<tr class='homepagetable'>";
    while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
        if($x<=3){
            $x = $x + 1;
            extract($row);
            echo "<td class='homepagetable'>";
            echo "<a href=itemdetails.php?id=$id>";
            echo '<img src=img/' . $image . ' style="max-width:220px;max-height:240px;width:220px;height:240px;"></img><br/>';
            echo '<div class="truncate">'. $name .'</div><br/>';
            echo "</a>";
            echo 'Rp.'.$price .'<br/>';
            echo "</td>";
        } else {
            $x=1;
            echo "</tr><tr>";
        }
    }
        echo "</table>";
    }

Thanks in advance!

1
  • fix your html. <img> is a singleton tag, and </img> doesn't exist. your attributes are also drunkenly jumping between quoted and unquoted. you also don't CLOSE the final table row, leaving you with <tr><td></td></table> or whatever. Commented Aug 16, 2016 at 16:29

2 Answers 2

1

Your problem is that you have a wrong condition here:

if($x <=3)... else{...}

Change the if/else to this:

if($x <3){$x++;}

//...do something

if($x == 3){
 $x = 1;
 //Close and open tr
 }

And you need to close the <img> tag, and the last <tr> tag outside of the loop

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

Comments

0

Perhaps you can write it like this:

function getData()
{
  global $connect;
  $query = 'SELECT id, name, category, price, image, info FROM product_data';
  $result = mysqli_query($connect, $query) or die(mysql_error());
  echo '<table border="0">';
  $x=0;
  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
  {
    if($x % 3 == 0) 
    {
      // number of articles is a multiple of 3 - so close the row
      if($x) echo '</tr>'; // but only if there was at least 1 article
      echo '<tr class="homepagetable">';
    }
    $x ++;
    echo '<td class="homepagetable"><a href="itemdetails.php?id='.$row['id'].'">
      <img src="img/'.$row['image'].'" style="max-width:220px;max-height:240px;width:220px;height:240px;"><br/>
      <div class="truncate">'.$row['name'].'</div><br/>
      </a>Rp. '.$row['price'].'<br/>
      </td>';
  }
  if($x) echo '</tr>'; // only close the row if there was at least 1 article
  echo '</table>';
}

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.