0

it is working nice.. but when i open the page.. its allways give me this message Undefined variable: list in C:\xampp\htdocs\shed\select.php on line 115

when i submit the page it allways gives me right data.. but allways shows me that undefined variable $list

   <?php
  include("db.php");
  if(isset($_POST['submit']))
  {


    $aa= mysql_real_escape_string($_POST['loco_name']);
    $bb=mysql_real_escape_string ($_POST['section']);

    $result=mysql_query("select * from loco_detail Where loco_name= '$aa' AND section= '$bb' ") or die (mysql_error());




      $Count = mysql_num_rows($result); // count the output amount
     if ($Count > 0)
          {
       while($row = mysql_fetch_array($result))
             { 
         $name = $row["loco_name"];
         $section = $row["section"];
         $schedule = $row["schedule"];
         $repair = $row["repair"];
         $action = $row["action"];

         $date = strftime("%b %d, %Y", strtotime($row["date"]));


     $list .="  


     <table id=\"table\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">
     <tr>
     <td id=\"row\">  $name     </td>
     <td id=\"row\">  $schedule  </td>
      <td id=\"row\">  $repair  </td>
      <td id=\"row\">  $action  </td>
      <td id=\"row\">  $date      </td>
        </tr>
     </table>

     ";      


    }
   } else {
     $list = "You have no products listed in your store yet";
   }



     }



     ?>

here the html code

        <body>



    <center>
     <fieldset>
    <legend>Select</legend>
    <table>
    <form method="post">
    <tr><td>Loco Number:</td><td><input type="text" name="loco_name"/></td></tr>
     <tr><td>Section:</td><td><select name="section">
     <option value="Mechanical">Mechanical</option>
     <option value="Electrical">Electrical</option>
     </select></td></tr>
     <tr><td><input type="submit" name="submit" value="Submit"></td></tr>
     </form>
     </table>
      </fieldset>
     </center>


     <div >

       <?php echo $list; ?>

       </div>


       </body>

please make me out from here.

komal deep singh chahal
  • 1,229
  • 3
  • 13
  • 28

1 Answers1

3

There is a period in front of $list which is invoking string concatenation.

The error message is saying you're trying to use a variable that hasn't been "declared" yet. However these are pretty pointless to fix and just causes you to write a lot more redundant code.

You can disable these in your php.ini by changing: error_reporting to E_ALL & ~E_NOTICE or adding error_reporting(E_ALL & ~E_NOTICE); to the top of the page

In a nut shell you're trying to add on to an existing variable called $list that was never declared.

Example of string concatenation:

You can read more here: PHP String Operators

$string = "Hello "; // Declared
$string .= "World";
$string .= "!";
echo $string;

// Outputs "Hello World!"

Basically to fix your issue change your if statement to the following

if ($Count > 0) {
    $list = ''; 
    while($row = mysql_fetch_array($result)) { 
        $name = $row["loco_name"];
        $section = $row["section"];
        $schedule = $row["schedule"];
        $repair = $row["repair"];
        $action = $row["action"];
        $date = strftime("%b %d, %Y", strtotime($row["date"]));

        ob_start();
        ?>
        <table id="table" border="1" cellpadding="2" cellspacing="2">
         <tr>
            <td class="row"><?=$name?></td>
            <td class="row"><?=$schedule?></td>
            <td class="row"><?=$repair?></td>
            <td class="row"><?=$action?></td>
            <td class="row"><?=$date?></td>
            </tr>
        </table>
        <?
        $list .= ob_get_clean();

        // rest of code here
    }
}
zanderwar
  • 3,440
  • 3
  • 28
  • 46