0

I have the following php code which queries a table of my database according to place. The table structure is as follows :

 s/n | inventory no |   asset no  |    place
=====+==============+============+=============
  1  |      125A    |    5245     |   London
  2  |     1254B    |    7545     |   London
  3  |      128A    |    5645     |  New York
  4  |      254B    |    1545     |    Tokyo
  5  |      6545    |    1456     |    Tokyo

And the code:

$location=$_POST['loc'];  
foreach ($location as $chk1)
{ 
   echo "<br>";
   $sql="SELECT * FROM desktop WHERE Place='$chk1';";
    $result=mysql_query($sql);


if($result==null)
 {
   echo '<script> alert("No entry for location '.$chk1.' anditem '.'desktopreturned");</script>';
continue;
 }
 $row = mysql_fetch_array($result);
 if($row==null)
 {
   echo '<script>alert("No entry for location '.$chk1.' and item '.'desktop returned");</script>';
  continue;
 }

   mysql_data_seek($result, 0);
  ?><table border='1' id="desktop" caption=<?php $location ?> >
 <tr>
 <th>S/N</th>
 <th>Inventory No</th>
 <th>Asset No</th>
 <th>Place</th>
 </tr>
  <?php
 while($row = mysql_fetch_array($result)) {?>
 <tr class="alt">
 <?php
    echo "<td>" . $row['S/N'] . "</td>";
    echo "<td>" . $row['Inventory no'] . "</td>";
    echo "<td>" . $row['Asset No'] . "</td>";
    echo "<td>" . $row['Place'] . "</td>";
    echo "</tr>";

  }

 echo "</table>";
}

Edit : CSS code :

  #desktop {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
width: 100%;
border-collapse: collapse;
}

#desktop th {
font-size: 1.1em;
text-align: left;
padding-top: 5px;
padding-bottom: 4px;
background-color: #FFCC33;
color: #ffffff;
}

#desktop td, #desktop th {
font-size: 1em;
border: 1px solid #FFCC33;
padding: 3px 7px 2px 7px;
}


#desktop tr.alt td {
color: #000000;
background-color: #FFFFCC;
}

Here, $location is storing the locations selected in the html form.

I want the returned result of the query for different places to be displayed as a single table. Currently it is displaying different place's result as different tables having their own table headers. I want the returned query to be appended into one variable.

1
  • 1
    You're vulnerable to SQL injection. Commented Jul 25, 2014 at 6:33

2 Answers 2

1

Just place your table before the foreach loop.As it is inside the loop it repeats each time the loop continues

<table border='1' id="desktop" caption=<?php $location ?> >
   <tr>
    <th>S/N</th>
    <th>Inventory No</th>
    <th>Asset No</th>
    <th>Place</th>
  </tr>
foreach ($location as $chk1)
  { 
    echo "<br>";
    $sql="SELECT * FROM desktop WHERE Place='$chk1';";
    $result=mysql_query($sql);
 ...
 ...

  } 
}
echo "</table>";
Sign up to request clarification or add additional context in comments.

3 Comments

For some reason the css is getting applied only on the first place's returned result. The rest of the result is getting displayed without any css and formatting.
can you post sample of the css?You have applied styles to alt class right?
You please try giving class to each column and apply css to the class.
0

try with this code, I have changed location array default to london,

you have changed case for field name first letter should not be capital. Hope this help :)

  $location=array('London','Tokyo'); 
 $str.='<table border="1" id="desktop">
 <tr>
 <th>S/N</th>
 <th>Inventory No</th>
 <th>Asset No</th>
 <th>Place</th>
 </tr>'; 
foreach ($location as $chk1)
{ 
   echo "<br>";
   $sql="SELECT * FROM desktop WHERE Place='$chk1';";
    $result=mysql_query($sql);


if($result==null)
 {
   echo '<script> alert("No entry for location '.$chk1.' anditem '.'desktopreturned");</script>';
continue;
 }
 $row = mysql_fetch_array($result);
 if($row==null)
 {
   echo '<script>alert("No entry for location '.$chk1.' and item '.'desktop returned");</script>';
  continue;
 }

   mysql_data_seek($result, 0);



 while($row = mysql_fetch_array($result)) {
 $str.='<tr class="alt">

        <td>' . $row['sr'] . '</td>
        <td>' . $row['inventory_no'] . '</td>
    <td>'. $row['asset_no'] . '</td>
    <td>' . $row['place'] . '</td>
    </tr>';

  }


}
$str.='</table>';
echo $str;
?>

10 Comments

Is there a way to append the result returned in each iteration into one variable?
Yes, append your table generation code in one variable and finally echo it. it will print entire table.
I don't understand what you are saying. I am still a beginner.
I want some way to append all the results returned in each iteration
above code will append result of each iteration in single table, that is what you want, isn't?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.