1

I am still very new to MySQL/PHP and am trying to make code that will loop through my whole SQL table. I was able to do that, to clean things up I wanted to use an html table to store the values in to make things look neater. I tried adding the HTML code into the string which gets printed at the bottom of the code in the HTML section, but the table borders do not show. What am I doing wrong and how can I fix this? Both code and screenshot of output are below:

<?php

    include("connection.php");

    $query= "SELECT * FROM schedule";

    $result = mysqli_query($link, $query);

    $scheduletext="<table>";

    if($result = mysqli_query($link, $query)) {

        while ($row=mysqli_fetch_array($result)) {

            $scheduletext="<tr><td>".$scheduletext.$row[1]."</td>";

            $scheduletext="<td>".$scheduletext.$row[2]."</td>";

            $scheduletext="<td>".$scheduletext.$row[3]."</td>";

            $scheduletext="<td>".$scheduletext.$row[4]."</td></tr>";

        }
    }
    $scheduletext=$scheduletext."</table>";

?>

<html>
    <head>

        <title>TastySnack - Production Schedule</title>

        <link href="https://fonts.googleapis.com/css?family=Kaushan+Script" rel="stylesheet">       

        <link rel="stylesheet" type="text/css" href="tasty.css">

    </head>

    <body>

        <div id="top">

            <div id="top-left">

                TastySnack Production

            </div>

            <div id="top-right">

                <img id="logo" src="images/TastysnackLogo.jpg">

            </div>

        </div>

        <div id="split"></div>

        <div id="schedule">

            <?php 

                print_r($scheduletext);

            ?>  

        </div>

    </body>


</html>

Click Here For Screenshot of Output

Brandon
  • 465
  • 3
  • 10
  • 19
  • 4
    use concatenation: `$scheduletext .=` (notice the dot) – mister martin Oct 19 '16 at 18:03
  • Look at your source code an you should see the issue. `` is not going to be right.
    – chris85 Oct 19 '16 at 18:14
  • @chris85 I see what you are saying! Why is it adding all the extras at the beginning though? 0000-00-0022oz Dark12ct 4oz Dark2016-01-01978524782016-01-02875421362016-01-03.........
    – Brandon Oct 19 '16 at 18:36
  • Because you are adding `` to the start of the variable on every iteration. Simple example, https://eval.in/663332, then multiple that by how many rows your have. Extended example: https://eval.in/663337 – chris85 Oct 19 '16 at 18:37
  • I am not familiar with eval.in, im assuming it is like jsbin? to be honest I dont fully understand my code, I am still learning, played around with it and it seemed to work for outputting the MySQL table. – Brandon Oct 19 '16 at 19:03
  • Yup, http://stackoverflow.com/questions/4616159/is-there-a-php-sandbox-something-like-jsfiddle-is-to-js. – chris85 Oct 19 '16 at 20:34

1 Answers1

0

As @mister martin said in comment use dot to concatinate your string

$scheduletext ="<table>";

if($result = mysqli_query($link, $query)) {

    while ($row=mysqli_fetch_array($result)) {

        $scheduletext .="<tr><td>".$scheduletext.$row[1]."</td>";

        $scheduletext .="<td>".$scheduletext.$row[2]."</td>";

        $scheduletext .="<td>".$scheduletext.$row[3]."</td>";

        $scheduletext .="<td>".$scheduletext.$row[4]."</td></tr>";

    }
}
$scheduletext .="</table>";
Sanooj T
  • 1,317
  • 1
  • 14
  • 25
  • Don't concatenate `$scheduletext`. – chris85 Oct 19 '16 at 18:16
  • That did not work for me. I got the fatal error: Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 116497061 bytes) in /home2/bweibley/public_html/www.kr3ativedesign.com/GnS/Index.php on line 19 – Brandon Oct 19 '16 at 18:33
  • http://stackoverflow.com/questions/4096582/allowed-memory-size-of-x-bytes-exhausted – Sanooj T Oct 20 '16 at 03:56