1
<?php  
    include_once 'config.php';  

    $query = $config -> prepare("SELECT `edit`, `user_banned`, `ban_reason`, `ban_time`, `user_banner`, `ban_timestamp` FROM `samp_ban` ORDER BY `edit` ASC LIMIT 10");  
    if($query -> execute())  
    {  
        $query_results = $query->fetchAll();
         if($ban_time == 0) { $query_result["ban_time"] = "Permanent"; } 

    }  
?>

code edidetcode edidetcode edidetcode edidet

ERROR: Undefined variable: ban_time

2
  • Fatal error: Cannot use [] for reading in Commented Mar 16, 2018 at 8:09
  • In your while loop where you fetch, echo a row with all surrounding HTML tags (<div class="row">) Commented Mar 16, 2018 at 8:11

3 Answers 3

1

You have to combine and html and php for getting all data from query

if($row_count)  
        {  
            while($query_result = $query -> fetch()){  
                 $Username = $query_result['Username']; 
                 $Headshots = $query_result['Headshots']; 
                 $ForumName = $query_result['ForumName'] ; 
                 ?>
                 <div class="row">
                            <div class="cell" data-title="Full Name">
                                <?php echo $Username ?>
                            </div>
                            <div class="cell" data-title="Headshots">
                                <?php echo $Headshots ?>
                            </div>
                            <div class="cell" data-title="Forum Title">
                                <?php echo $ForumName ?>
                            </div>
                        </div>
                 <?php
                }
        }
Sign up to request clarification or add additional context in comments.

5 Comments

syntax error, unexpected '<' I removed all cell div's and added yours, only 1, but error is shown
edit the question and put what you have tried and which line you are getting error
Done @Vishnu Bhadoriya
you are not closing php tag ?> add the ?> @ line 72
Thank you for your help and time !
0

The issue is that you are trying to output same variables on both rows. As a result you get two rows with same results. You have to store rows from database to array and then make a for loop to output your html with data from that array.

PHP code

<?php  
    include_once 'config.php';  

    $query = $config -> prepare("SELECT `Username`, `Headshots`, `ForumName` FROM `users` ORDER BY `Headshots` DESC LIMIT 10");  

    if($query -> execute())  
    {  
        $query_results = $query->fetchAll();
    }  
?>

HTML code

<div class="limiter">

    <div class="container-table100">
        <div class="wrap-table100">
            <div class="table">

                <div class="row header">
                    <div class="cell">
                        Nickname
                    </div>
                    <div class="cell">
                        Headshots
                    </div>
                    <div class="cell">
                        Forum Name
                    </div>
                </div>

                <?php foreach( $query_results as $query_result ) { ?>
                <div class="row">
                    <div class="cell" data-title="Full Name">
                        <?php echo $query_result["Username"]; ?>
                    </div>
                    <div class="cell" data-title="Headshots">
                        <?php echo $query_result["Headshots"]; ?>
                    </div>
                    <div class="cell" data-title="Forum Title">
                        <?php echo $query_result["ForumName"]; ?>
                    </div>
                </div>
                <?php } ?>

5 Comments

I have a question, I want to add. if($Username == 0) { $ban_time = "Yes"; } else if($Username == 1) { $Username= "no"; }
@NewbieHTML, I would also suggest to always consider security when outputting text to HTML. <?php echo $query_result["Username"]; ?> is dangerous if Username is allowed to contain special characters used in HTML markup or javascript. In such cases as this I would really recommend using htmlspecialchars(): <?php echo htmlspecialchars($query_result["Username"]); ?>. That way if for example username is <B>ill you will see the real user name instead of bold ill.
Well, thank you, but it gives me an error. Cannot use object of type PDOStatement as array
You probably used $query variable instead of $query_result. In my suggested code $query is PDOStatement, $query_results is an array of rows (associative arrays) and $query_result is an associative array with row data (Username, Headshots, ForumName). Make sure you use correct variables :)
You should keep original question and ask a new question if you need further assistance with your changing code. Currently your issue is that there is no $ban_time variable. You should put your conditional code in the for loop and use $query_result variable. Put <?php if($query_result["ban_time"] == 0) { $query_result["ban_time"] = "Permanent"; } ?> after <?php foreach( $query_results as $query_result ) { ?>. $query_result is available ONLY in the for loop that iterates through $query_results array.
0

You need to change

<?php  
    include_once 'config.php';  

    $query = $config -> prepare("SELECT `Username`, `Headshots`, `ForumName` FROM `users` ORDER BY `Headshots` DESC LIMIT 10");  

    if($query -> execute())  
    {  
        $row_count = $query -> rowCount();  

        if($row_count)  
        {  
            while($query_result = $query -> fetch())  
                 $Username = $query_result['Username']; 
                 $Headshots = $query_result['Headshots']; 
                 $ForumName = $query_result['ForumName'] ; 
        }  
    }  
?>

into

<?php  
    include_once 'config.php';  

    $query = $config -> prepare("SELECT `Username`, `Headshots`, `ForumName` FROM `users` ORDER BY `Headshots` DESC LIMIT 10");  

    if($query -> execute())  
    {  
        $row_count = $query -> rowCount();  

        if($row_count)  
        {  
            while($query_result = $query -> fetch()) 
            { 
                 $Username = $query_result['Username']; 
                 $Headshots = $query_result['Headshots']; 
                 $ForumName = $query_result['ForumName'] ; 
                 echo '<div class="row">';
                        echo '<div class="cell" data-title="Full Name">';
                             echo $Username;
                        echo '</div>';
                        echo '<div class="cell" data-title="Headshots">';
                            echo $Headshots;
                        echo '</div>';
                        echo '<div class="cell" data-title="Forum Title">';
                            echo $ForumName;
                        echo '</div>';
                     echo '</div>';
            }
        }  
    }  
?>

You forgot the { } arround the while loop

1 Comment

Oh lol, yeah. Thank you. But now it remains only one problem, I need to echo 9 more user's data, way made by me is only for 1 user, in the initial script was for all 10 users but they were shown only in 1 row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.