Doubt with SQL/PHP

Asked

Viewed 30 times

2

I have the following query:

$qrysel = "select * from pack";
$ressel = mysqli_query($db, $qrysel);
$obj = mysqli_fetch_object($ressel);

In html I have this:

<?= $obj['pack_name']; ?>

How could I make it show the value according to a certain id, without using the WHILE and without putting the WHERE in the query...

It is possible to define the WHERE directly in html obj?

Table:

<table>
  <tbody>
    <tr>
      <td>item 1</td>
      <td>item 2</td>
      <td>item 3</td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
      <td>item 4</td>
      <td>item 5</td>
      <td>item 6</td>
    </tr>
  </tbody>
</table>

If I were to use the while it would repeat all the TD then I need it to be displayed like this one above.

There are exactly 6 records in this table.

  • 1

    There’s no way. You’d have to use the where or in the worst case do a while and make the comparison record the record.

  • Try to talk more about what you want to do, we can help with other ideas

  • So, I can’t use the while because it goes from the problem with the display, as I have to display 3 items per row in a table.. with the while it will display all in a row.

  • 2

    It will be a problem because you probably have no experience working HTML+PHP, if you explain better what you have already done I’m sure you have how to solve.

  • I updated the question, take a look if you can understand what I need to do..

  • 1

    use a foreach($Ressel as $obj){if($obj['id']=1){echo"<td>$obj['name']</td>";}Else {echo "no record";} }

  • @Tulip tree I do not know how to assemble the structure using this method, if you can provide some example please..

  • 1

    @Eita this worked perfectly, I did not know this function. Thank you very much :D

  • @Wendler published a more detailed answer there that good that I helped you face I am very happy if you can vote in my publication and mark as answer is correct thank you!

Show 4 more comments

1 answer

3


first of all you make the querry

$qrysel = "select * from pack";
$ressel = mysqli_query($db, $qrysel);
$obj = mysqli_fetch_object($ressel);

then assemble the table

                                <table>
                                    <thead>
                                        <tr>
                                                <th>id</th>
                                                <th>nome</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                                foreach($ressel as $obj){ ?>
                                                <tr>
                                                   <?php if($obj['id']==1){echo "<td>"  .$obj['id']. "</td>";} else{ echo"sem registro";}?>
                                                   <?php if($obj['id']==1){echo "<td>"  .$obj['nome']. "</td>";}else{echo"sem registro";}?>
                                                </tr>
                                        <?php     }                                ?>
                                    </tbody>
                                </table>

try to do this here I think it will solve your problem!

  • 1

    Show, it worked right :)

  • I’m happy @Wendler anything is just talking tmj!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.