1

I am having trouble looping the mysql data with html for a limited amount of rows. I need the echo the initial div's in the html to display the initial html containers that do not need to be looped. After, I want to loop the first 4 rows returned along with its html block. I am coming up with an empty page. It is not displaying the 4 rows with the mysql data.

    function functionName(){

        $DBvar = new mysqli($Serv,$use,$pass,$db);

        /* Checking Connection */
        if($DBvar->connect_errno) {
            printf("Connection Failed:  %s \n",$DBvar->connect_error);
            exit();
        }

        if($response = $DBvar->query("SELECT * FROM Table1 ORDER BY 'ColunmName' DESC LIMIT 0,4")) {

            echo "<div class=\"row\">
                    <div class=\"span12\">
                        <div class=\"recent-posts\"><br>";

            while($obj = $response->fetch_object()) {
                $id = $obj['id'];
                $clmn1= $obj['clmn1'];
                $clmn2= $obj['clmn2'];
                $clmn3= $obj['clmn3'];

                printf("<div class=\"span3 border-hover\" id=\"id%s\">
                            <article>
                            <h5><a rel=\"nofollow\"><strong>%s</strong></a></h5>
                            %s
                            </article>
                            <p>%s</p>
                        </div>", $id, $clmn1, $clmn2, $clmn3);
            }

            echo "
                </div>
                </div>
                </div>";
        }

        $response->close();
    }
Barmar
  • 741,623
  • 53
  • 500
  • 612
Rookie Recruits
  • 93
  • 3
  • 18
  • chances are your query is wrong possibly because of the `'` surrounding your `ORDER BY` column – cmorrissey May 01 '15 at 20:11
  • That will prevent it from ordering the rows properly, but won't cause an empty page. – Barmar May 01 '15 at 20:13
  • Add an `else die($DBvar->error)` to see the error if the query failed. – Barmar May 01 '15 at 20:14
  • Regarding the quotes, see http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks – Barmar May 01 '15 at 20:16
  • Add `error_reporting(E_ALL)` to the beginning of your script, and also check your PHP error log for messages. – Barmar May 01 '15 at 20:17
  • Thanks checking out your suggestions. I go an error that went away when these were deleted $id = $obj['id']; $clmn1= $obj['clmn1']; $clmn2= $obj['clmn2']; $clmn3= $obj['clmn3']; – Rookie Recruits May 01 '15 at 21:58
  • Guess I didn't need them after all. – Rookie Recruits May 01 '15 at 21:59
  • @RookieRecruits: If you found an answer to your question, please formulate it below (answer your own question). You can later then mark your question as answered. – hakre May 03 '15 at 07:07
  • I don't see anywhere in your script where you have done this, but if you are assigning values to variables in a loop, typically you want to specify those variables as an array. i.e. `$id[]`, `$clmn1[]`, `$clmn2[]`, ... – EternalHour May 05 '15 at 00:59

1 Answers1

0

I ended up finding a solution writing the code in this manner. So I did not need the extra variables after the while statement. but I know there is a better way to rewrite this so that I can continue to pull data from the DB and dynamically generate another row with 4 more items that follow. Right now I had to double to code for 2 or 3 rows. It should be more efficient than this. It is a good short term solution but not long term or even scalable. Thanks for everyone's help!

function FunctionName(){

        $DBConnect = new mysqli(SERVER,USER,PASSWORD,DATABASE);

        /* Checking Connection */
        if($DBConnect->connect_errno) {
            printf("Connection Failed:  %s \n",$DBConnect->connect_error);
            exit();
        }

        if($result = $DBConnect->query("SELECT * FROM TableName LIMIT 0,4")) {
                //Then I echoed the initial html to create the column row since I only need 1 row per 4 items to display.
                echo "<div class=\"row\">
                        <div class=\"span12\">
                            <div class=\"recent-posts\"><br>
                                <div class=\"row\">
                                    <div class=\"\">
                                        <ul class=\"slides\">
                                            <li>";

                                                while($obj = $result->fetch_object()) {
                                                            printf("<div class=\"span3 border-hover\" id=\"$var%s\">
                                                                        <article>
                                                                        <h5><a rel=\"nofollow\"><strong>%s</strong></a></h5>
                                                                        %s
                                                                        </article>
                                                                        <p>%s</p>
                                                                    </div>", $obj->$var, $obj->$var1, $obj->$var2, $obj->$var3);
                                        }
                echo "                      </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>";
            }
Rookie Recruits
  • 93
  • 3
  • 18