Jump to content

Problem with Mysql data to html table


Recommended Posts

 

Hello, I have the problem that only one user is displayed in the table

require('connection/db1.php');
// Teilnehmerliste
$query = 'SELECT * FROM convoy_part WHERE user_convoy= :I';
$start = $bdd->prepare($query);
$start->execute(array(':I' => $_GET['id']));

//fetch
$result2 = $start->fetch();
// Zählung der Datensätze
$count = $start->rowCount();
<table class="table">
                                <thead>
                                    <tr>
                                    <th scope="col">User</th>
                                    <th scope="col">Datum</th>
                                    <th scope="col"><a class="btn btn-primary" href="convoy_user.php?id=<?php echo $result['id']; ?>&action=part" role="button">Teilnehmen</a></th>
                                    </tr>
                                </thead>
                            <tbody>
                        <tr>
                            <th scope="row"><?php echo $result2['name']; ?></th>
                            <td><?php echo $result2['date']; ?></td>
                        </tr>
                  </tbody>
                </table>

 

Link to comment
https://forums.phpfreaks.com/topic/311357-problem-with-mysql-data-to-html-table/
Share on other sites

You need to put the output in a loop. Fetch only gets 1 row. Your HTML only outputs 1 row. You need to move the fetch and loop on it where you start a new row.

Also, don't use * in your select. List only those columns you will actually use. Since you don't use '$count' you don't need that statement.

 

<?php
require('connection/db1.php');
// Teilnehmerliste
$rows = $result2->fetchAll(PDO::FETCH_ASSOC)
foreach ($rows as $key=>$value) {
$query = 'SELECT * FROM convoy_part WHERE user_convoy= :I';
$start = $bdd->prepare($query);
$start->execute(array(':I' => $_GET['id']));

//fetch
$result2 = $start->fetch();
// Zählung der Datensätze
$count = $start->rowCount();
}

 

 

1 hour ago, Endrick said:

<?php
require('connection/db1.php');
// Teilnehmerliste
$rows = $result2->fetchAll(PDO::FETCH_ASSOC)
foreach ($rows as $key=>$value) {
$query = 'SELECT * FROM convoy_part WHERE user_convoy= :I';
$start = $bdd->prepare($query);
$start->execute(array(':I' => $_GET['id']));

//fetch
$result2 = $start->fetch();
// Zählung der Datensätze
$count = $start->rowCount();
}

 

What is wrong in my code?

Edited by Guest

there are three current problems -

1. you are getting a fatal runtime error, due to both where you put the php statements and that one of the statements is using a wrong variable name, but you don't have php's error related settings set up so that php will help you. when learning, developing, and debugging code/query(ies), you need to display all php errors. find the php.ini that php is using and set error_reporting to E_ALL and set display_errors to ON. stop and start your web server to insure that any changes made to the php.ini will take effect.

2. where you put those lines of code is nonsense. you put them before the point where you are executing the sql query. this says you are not even looking at what you are doing. you must be aware of what each line of code is doing and what it is contributing to the overall goal. the first line of code, which is attempting to fetch all the rows of data from the query, should replace the current line of code that's fetching just one row of data. it goes in your program at the point where you are fetching the data from the query, which would be after the point where you are executing the query. the second line of code is the start of the loop. it would go at the point in the html document where you are going to repeat the output for each user. you would 'close' the loop, with a } at the end of the block of html that you are repeating for each user. :facepalm:

3. the first line of code you copied, is using a wrong variable name. copying code is not programming. that's just you acting like a human Optical Character Reader (OCR) program. again, you must actually look at and read what the lines of code are doing. in your code, the $start variable (which is poorly named, use something like $stmt to indicate the variable holds a PDOStatement object), is what you would be calling the ->fetchAll() method on.

https://prnt.sc/u33ihn  => translated this means "This page does not work" "Can't process this request at the moment."

<?php
require('connection/db1.php');
// Teilnehmerliste


$query = 'SELECT * FROM convoy_part WHERE user_convoy= :I';
$start = $bdd->prepare($query);
$start->execute(array(':I' => $_GET['id']));
$rows = $result2->fetchAll(PDO::FETCH_ASSOC)
foreach ($rows as $key=>$value) {
//fetch
$result2 = $start->fetch();
// Zählung der Datensätze
$count = $start->rowCount();
}

 

<?php
require('connection/db1.php');
// Teilnehmerliste


$query = 'SELECT * FROM convoy_part WHERE user_convoy= :I';
$start = $bdd->prepare($query);
$start->execute(array(':I' => $_GET['id']));

//fetch
$result2 = $start->fetch();
$rows = $result2->fetchAll(PDO::FETCH_ASSOC)
foreach ($rows as $key=>$value) {
// Zählung der Datensätze
$count = $start->rowCount();
}

@gw1500se what am I doing wrong

Edited by Guest
$query = 'SELECT * FROM convoy_part WHERE user_convoy= :I';
$start = $bdd->prepare($query);
$start->execute(array(':I' => $_GET['id']));
$rows = $start->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    // Zählung der Datensätze
    echo "<pre>";
    print_r($row);
    echo "</pre>";  
}

 

Everything works now, but I want to do it in cards instead of in a table. There I have the problem that the cards do not line up

 <section class="no-padding-top">
           <?php
                            $query = 'SELECT * FROM convoy_part WHERE user_convoy= :I';
                            $start = $bdd->prepare($query);
                            $start->execute(array(':I' => $_GET['id']));
                            $rows = $start->fetchAll(PDO::FETCH_ASSOC);
                            foreach ($rows as $row) {
                                
                             
                            
                            echo '
           
             <section class="no-padding-bottom">
          <div class="container-fluid">
            <div class="row">
            
              <div class="col-lg-4">
                <div class="user-block block text-center">
                  <div class="avatar"><img src="https://d19m59y37dris4.cloudfront.net/dark-admin/1-4-6/img/avatar-1.jpg" alt="..." class="img-fluid">
                    <div class="order dashbg-2">1st</div>
                  </div><a href="#" class="user-title">
                    <h3 class="h5">Richard Nevoreski</h3><span>@richardnevo</span></a>
                  <div class="contributions">950 Contributions</div>
                  <div class="details d-flex">
                    <div class="item"><i class="icon-info"></i><strong>150</strong></div>
                    <div class="item"><i class="fa fa-gg"></i><strong>340</strong></div>
                    <div class="item"><i class="icon-flow-branch"></i><strong>460</strong></div>
                  </div>
                </div>
              </div>
            </div>
           
            </section>';
                           
                           
                          
                             } ?>
           

           </section>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.