How to generate HTML table with nested loop? - php

I have a table that should look like the following:
http://hell-rider.de/Fotos/img/beispiel-1.JPG
I have the following PHP script:
foreach ($xml->result->rowset->row as $row){
if($row{'refTypeID'} == 42){
echo '<tr>';
echo "<th>";
echo $row{'date'};
echo "</th>";
echo "<th>";
echo '' . $row{'ownerName1'} .'';
echo "</th>";
echo "<th>";
foreach ($allgemeinxml->result->rowset->row as $type){
echo '' . $type{'typeName'} .'';
}
echo "</th>";
echo "<th>";
echo '<p class="minus">' . $row{'amount'} .' ISK</p>';
echo "</th>";
echo "<th>";
echo '<p class="kontostand">' . $row{'balance'} . ' ISK</p>';
echo "</th>";
echo '</tr>';
}
}
echo "</table>";
The actual output of my script is the following, however:
http://hell-rider.de/Fotos/img/beispiel2.JPG
How do I have to change my script to populate the third column correctly (and not with MetallurgyTradeLaboratory OperationLaboratory OperationLaboratory OperationResearchCybernetics)?

<th> tags are for header cells, you should be using <td> data cells.
$types = array();
foreach ($allgemeinxml->result->rowset->row as $type){
$types[] = $type{'typeName'};
}
$type_index = 0;
foreach ($xml->result->rowset->row as $row){
if($row{'refTypeID'} == 42){
echo '<tr>';
echo "<td>";
echo $row{'date'};
echo "</td>";
echo "<td>";
echo '' . $row{'ownerName1'} .'';
echo "</td>";
echo "<td>";
echo $types[$type_index];
echo "</td>";
echo "<td>";
echo '<p class="minus">' . $row{'amount'} .' ISK</p>';
echo "</td>";
echo "<td>";
echo '<p class="kontostand">' . $row{'balance'} . ' ISK</p>';
echo "</td>";
echo '</tr>';
}
$type_index++;
}
echo "</table>";
I don't know exactly what the use of if($row{'refTypeID'} == 42){ is, so you either want $type_index++; in the place I've put it above, or just above the closing brace }. For this to work well, $allgemeinxml->result->rowset->row and $xml->result->rowset->row need the same number of elements.

Related

How do I get my SQL results to appear in one table instead of two within PHP/HTML

I have a PDO connection to a database that returns any results for an item number that is searched. It currently returns each instance in its own table but i would like to see all instances in one table. Here is my code.
if (count($resultssample) > 0) {
echo "<br>";
echo "Number in the business: ".count($resultssample)."<br>";
echo "<br>";
foreach ($resultssample as $r) {
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>site name</th>";
echo "<th>location name</th>";
echo "<th>creation time</th>";
echo "<th>creation date</th>";
echo "<th>damage status</th>";
echo "<th>colour</th>";
echo "<th>inventory status</th>";
echo "<th>notes</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
echo "<td>". $r['site_name'] ."</td>";
echo "<td>". $r['location_name'] ."</td>";
echo "<td>". $r['CreationTime'] ."</td>";
echo "<td>". $r['CreationDate'] ."</td>";
echo "<td>" . $r['damage_status'] . "</td>";
echo "<td>" . $r['colour'] . "</td>";
echo "<td>" . $r['inventory_status'] . "</td>";
echo "<td>" . $r['Notes'] . "</td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
}
} else {
echo "No results found";
}
How can this be achieved I cant currently get my head around it.
Any help would be greatly appreciated.
All you have to do is move table header out of the foreach loop:
if (count($resultssample) > 0) {
echo "<br>";
echo "Number in the business: ".count($resultssample)."<br>";
echo "<br>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>site name</th>";
echo "<th>location name</th>";
echo "<th>creation time</th>";
echo "<th>creation date</th>";
echo "<th>damage status</th>";
echo "<th>colour</th>";
echo "<th>inventory status</th>";
echo "<th>notes</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($resultssample as $r) {
echo "<tr>";
echo "<td>". $r['site_name'] ."</td>";
echo "<td>". $r['location_name'] ."</td>";
echo "<td>". $r['CreationTime'] ."</td>";
echo "<td>". $r['CreationDate'] ."</td>";
echo "<td>" . $r['damage_status'] . "</td>";
echo "<td>" . $r['colour'] . "</td>";
echo "<td>" . $r['inventory_status'] . "</td>";
echo "<td>" . $r['Notes'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
} else {
echo "No results found";
}

Echo Mysql into a table - presentation and DOM

Two questions in one:
First question, mainly about presentation:
I'm echo-ing the following code which should create a table. The table should have a single column, but it's being rendered with the elements above the image as a single line. can anyone see why?
<?php
$sql = "SELECT * FROM catdata WHERE featured='yes' LIMIT 2";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Unable to execute $sql. " . mysqli_error($link);
}
?>
Question 2:
Can I show the second result in a second column, or as a separate table? Also, is it possible to access the $result elements like say, $[manufacturer][1]?
Always look at the emitted source your code generates by either "View Source" or using a tool like curl. You'll find this mistake:
echo "</tr";
You're missing a >.
Many people who write HTML have browser plugins that can link through to an HTML validator to ensure they've got the correct syntax. You may want to find and install one of these.
To generate the second result is a separate table follow the following code.
echo "<table>";
$count = 1;
while($row = mysqli_fetch_array($result)){
if($count == 2){
echo "<table>";
echo "<tr>";
echo "<td>put your code here</td>";
echo "</tr>"
echo </table>
}else{
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
$count ++;
}
echo "</table>";
As you are limiting your query to only two record so this method is not bad for that and hope this will help.

ID'd link on click

I'm creating a contact based system, i have a contact list and want it to go to a full contact page when i click on a button, but the variable is being detected as a function?
<div id="po2" style="Margin:10% ">
<h1>Contacts</h1>
<?php
$sql = "SELECT * FROM `contacts`";
$query = mysqli_query($conn, $sql);
echo '<table class="data-table">';
echo'<thead>';
echo'<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo'</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td>'.$row['Forename'].'</td>';
echo '<td>'.$row['Surname'].'</td>';
echo $var==$row['Forename']("<td><a href='View.php?ID= " . urlencode($var) ."'>
<button type='button'>link</button>
</a></td>");
echo '</tr>';
}
echo'</tbody>';
echo '</table>';
?>
</div>
You are using a comparison of $var and the $row. Try setting $var to the $row each loop iteration.
echo '<thead>';
echo '<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
$var = $row['Forename'];
echo '<tr>';
echo '<td>' . $var . '</td>';
echo '<td>' . $row['Surname'] . '</td>';
echo "<td><a href='View.php?ID=" . urlencode($var) . "'><button type='button'>link</button></a></td>";
echo '</tr>';
}
echo '</tbody>';

Why does PHP nested foreach loop displays only first row from sql database

I am trying to display a table from database using two nested foreach loops with some criteria. here is the code-
echo "<div class='container'>";
echo "<table class='table table-hover table-bordered table-condensed' style='width:95%' align='center'>";
echo "<tr>";
echo "<th>";
echo "Edit";
echo "</th>";
echo "<th>";
echo "Delete";
echo "</th>";
echo "<th>";
echo "Sl. No.";
echo "</th>";
echo "<th>";
echo "Group";
echo "</th>";
echo "<th>";
echo "Component";
echo "</th>";
echo "<th>";
echo "Quantity";
echo "</th>";
echo "</tr>";
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
foreach($rslt3 as $item3)
{
/*echo $item3['component'];
if($item3['component']===$item['component'])
{
if($Qty>=$item3['Qty'])
{
$item3[Qty]=$item3[Qty]-$item[Qty];
*/
//will implement this after the second loop starts working
$id = $item['entry_id'];
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
echo "<tr>";
echo "<td>";
echo "<a href='production_edit.php?id=$id&Qty=$Qty&group_ID=$group_ID&component=$component&vendor_ID=$vendor_ID'>Edit</a>";
echo "</td>";
echo "<td>";
echo "<a href='production_delete.php?id=$id&vendor_ID=$vendor_ID'>Delete</a>";
echo "</td>";
echo "<td>";
echo $item['entry_id'];
echo "</td>";
echo "<td>";
echo $item['group_ID'];
echo "</td>";
echo "<td>";
echo $item['component'];
echo "</td>";
echo "<td>";
echo $item['Qty'];
echo "</td>";
echo "</tr>";
}
}
}
echo "</table></div><br>";
}
Now the problem here is when I use the second foreach loop the table displays the first entry in each table row... I am curious where I am at fault with this second foreach loop.. Thanks in advance

Finding query in substring instead of full text

I have created a search function. It can find address by the full text but how do I go about to make the query search by parts of the address ? For e.g. the full address is paya lebar Road Blk27, how do I make it so that the user can just type in paya and it would still show up ?
SearchForm
<h2>View Patient Records</h2>
<body>
<form action="display_patient.php" method="post">
<p>Select:
<select name="patient_var" >
<?php
$value = array(view_all, name, address);
foreach ($value as $option)
{
echo '<option value="'.$option.'"' . (isset($_POST['patient_var']) && $_POST['patient_var'] == $option ? ' selected' : '') . '>' . $option . '</option>';
}
?>
</select>
<input type="text" name="typed" value="" />
<input type ="submit" value="submit" />
</form>
</p>
<p>
<?php
if (isset($_POST['patient_var'])) {
$type = $_POST['typed'];
$select = $_POST['patient_var'];
if ($select == 'view_all') {
echo "<table border='1'>";
echo "<tr>\n";
echo "<th>ID</th>\n";
echo "<th>Patient Name</th>\n";
echo "<th>Age</th>\n";
echo "<th>NRIC</th>\n";
echo "<th>Birth Date</th>\n";
echo "<th>Medical Allergies</th>\n";
echo "<th>Medical History</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Address</th>\n";
echo "<th>Doctor Assigned</th>\n";
echo "</tr>";
$pat_set = default_patient();
while ($mo = mysqli_fetch_array($pat_set)) {
echo "<tr>";
echo "<td>" . $mo['id'] . "</td>";
echo "<td>". $mo['name'] . "</td>";
echo "<td>". $mo['age'] . "</td>";
echo "<td>". $mo['nric'] . "</td>";
echo "<td>". $mo['birthdate'] . "</td>";
echo "<td>". $mo['medical_allergies'] . "</td>";
echo "<td>". $mo['medical_history'] . "</td>";
echo "<td>". $mo['phone'] . "</td>";
echo "<td>". $mo['address'] ."</td>";
echo "<td>". $mo['doctor_assigned'] . "</td>";
echo "</tr>";
}
}
else {
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>ID</th>\n";
echo "<th>Patient Name</th>\n";
echo "<th>Age</th>\n";
echo "<th>NRIC</th>\n";
echo "<th>Birth Date</th>\n";
echo "<th>Medical Allergies</th>\n";
echo "<th>Medical History</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Address</th>\n";
echo "<th>Doctor Assigned</th>\n";
echo "</tr>";
$patients_set =
find_patients($select, $type);
while ($row = mysqli_fetch_array($patients_set))
{ echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>". $row['name'] . "</td>";
echo "<td>". $row['age'] . "</td>";
echo "<td>". $row['nric'] . "</td>";
echo "<td>". $row['birthdate'] . "</td>";
echo "<td>". $row['medical_allergies'] . "</td>";
echo "<td>". $row['medical_history'] . "</td>";
echo "<td>". $row['phone'] . "</td>";
echo "<td>". $row['address'] ."</td>";
echo "<td>". $row['doctor_assigned'] . "</td>";
echo "</tr>"; }
}
}//end of if post submit
?>
</p>
In Mysql For searching whether a column has specific string use LIKE
Example
SELECT * FROM table WHERE column LIKE '%somestring%';
In your case just try this
$typed = $_POST['typed'];
and make Mysql query like this
$query = "select * from table where Address LIKE '%".$typed."%' ";
Use LIKE '%$searchParam%' instead of = $searchParam
You forgot to post your function: find_patients($select, $type);
But I guess you need a partial string query for an address varchar field which looks something like this:
select * from MyTable where myColumn like '%myPartialString%';
This type of query will return all rows that have "MyPartialString" within.

Categories