Displaying MYSQL data using PHP & HTML - php

I'm a PHP beginner learner and I want to display all the results in a vertical way. For some reason, when there are multiple results, it just puts each result next to the other and not one over the other.
Any help will be highly appreciated.
This is my code
include "conexiondb.php";
if (isset($_POST['search'])) { // Search is the "name" attribute in the HTML input. The ones used with the "$_POST"
$busca = mysqli_real_escape_string($con, ($_POST['search'])) ;
$criteria = mysqli_real_escape_string($con, ($_POST['criteria'])) ;
if ($busca!="") {
$busqueda=$con->query("SELECT * FROM members WHERE {$criteria} = '{$busca}' ");
if ($busqueda === false) {
die('Could not connect:'.mysql_error()); // TODO: better error handling
}
}
echo "<div id ='tablennvoltura'><table border='1'>
<tr>
<th>Name</th>
<th>Address</th>
<th>Town</th>
<th>Zip</th>
<th>Cellphone</th>
<th>Birthday</th>
<th>Email</th>
<th>id</th>
<th>Editar</th>
<th>Nueva orden</th>
<th>Ver Ordenes</th>
</tr>";
while ($muestra=$busqueda->fetch_array()) {
echo '<td>'.$muestra['name'].' </td>';
echo '<td>' .$muestra['address']. '</td>';
echo '<td>' .$muestra['town']. '</td>';
echo '<td>' .$muestra['zip']. '</td>';
echo '<td>' .$muestra['cellphone']. '</td>';
echo '<td>' .$muestra['birthday']. '</td>';
echo '<td>' .$muestra['email']. '</td>';
echo '<td>' .$muestra['id']. '</td>';
echo "<td>"."See member"." </td>";
echo "<td>"."Nueva Orden"."</td>";
echo "<td>"."Ordenes"."</td>";
}
echo "</table></div>";
}
echo "</form>";

Because you place each variable into a table cell, but the cells are not encapsulated into a row (<tr>... </tr>).
while ($muestra=$busqueda->fetch_array()) {
echo '<tr>';
...
echo '</tr>';
}
Actually, this question does not have too much to do with php, it is purely an html issue.

Related

Update value if the checkbox if checked multiple rows php

I am trying to update each row with a value of 1 if the checkbox is checked and do nothing if not.
$query= "SELECT Name, Surname, Age, Club, age_group, School, team_select FROM players WHERE Age < 9";
$statement = $db->prepare($query);
$statement->execute();
$players = $statement->fetchAll(PDO::FETCH_ASSOC);
echo '<form nethod="POST" action="add_to_team.php">';
echo '<p align="center"><a href="new.php" >Add Player</a></p>';
echo '<table class="table table-bordered"';
echo '<tr><th>Name</th>
<th>Surname</th>
<th>Club</th>
<th>Age Group</th>
<th>School</th>
<th>Edit</th>
<th>Delete</th>
<th>Add to Team</th></tr>';
// loop through results of database query, displaying them in the table
foreach ($players as $player) {
// echo out the contents of each row into a table
echo '<tr>';
echo '<td>' . $player['Name'] . '</td>';
echo '<td>' . $player['Surname'] . '</td>';
echo '<td>' . $player['Club'] . '</td>';
echo '<td>' . $player['age_group'] . '</td>';
echo '<td>' . $player['School'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo '<td><input type="checkbox" name="team_select" value="1"> </td>';
echo '</tr>';
}
echo '</table>';
echo'<input type="submit" value="Submit" name="submit"><br/>';
Code (It just won't work)
<?php
include 'connect.php';//database connection
isset($_POST['team_select'])
?>
You need to give the checkboxes different names. If they're all named team_select there will just be one $_POST['team_select'], but you won't be able to tell which checkboxes were checked.
Use name="team_select[]" and they'll all be put into an array. Then you can put the player name into the value, so the array will contain the names of all the players who should be added.
echo '<td><input type="checkbox" name="team_select[]" value="' . $player['Name'] . '"> </td>';
When you're processing the form, you can do:
foreach ($_POST['team_select'] as $name) {
...
}
to process all the players who were selected.

How do I organize data in a PHP search query?

I'm in the process of making a PHP website, where a user can input data, then search through it later, but I can't seem to organize the data :/
Heres my code:
<form action="uploads.php" method="GET"><input id="search" type="text" placeholder="Type here"><input id="submit" type="submit" value="Search"></form></body></html>
When a user searched for their name, it results as so:
firstname=Mickey lastname=Mouse item1= item2= item3= item4= item5= item6=
Is there any way I can add a CSS or something to get the entries to line break or seperate?
In Your display page while displaying the data you can use the <br> tag so that it will display each and every data in different line.
First Name: <?php echo $loopvariable['fname'].'<br />'; ?>
Last Name: <?php echo $loopvariable['lname'].'<br />'; ?>
Like this you can provide for all the data which you print.
Output:
First Name: Name One
Last Name: Name Two
And you can provide as such information using the break tags in separate lines.
Basically in PHP you can echo html code so you can echo <br> statements as in the previous answer you can also echo css and you can create a block of code that you include
<?php
include('SomeMoreCode.php');
?>
You can also echo formatting commands to create a table. Just place the html statements in'' and join html and mysql/php values with .
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> <th></th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['hometeam'] . '</td>';
echo '<td>' . $row['fthg'] . '</td>';
echo '<td>' . $row['ftag'] . '</td>';
echo '<td>' . $row['awayteam'] . '</td>';
echo '<td>Edit</td>';
echo "</tr>";
}
echo "</table>";
non mysql - php only
<?php
$date='19/6/16';
$hometeam='Man United';
$fthg='3';
$ftag='2';
$awayteam='Man City';
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> </tr>";
echo "<tr>";
echo '<td>' . $date . '</td>';
echo '<td>' . $hometeam . '</td>';
echo '<td>' . $fthg . '</td>';
echo '<td>' . $ftag . '</td>';
echo '<td>' . $awayteam . '</td>';
echo "</tr>";
echo "</table>";
?>

How can I add a class for td in reference to two columns

I have a render of row of database
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
And in the tex1 is a date, my problem is for a class for tr, when the tex2 is empty and the date of tex1 is older than 30 days, tr have class red, the date of tex1 is inserted manually,how can I do to change the class on the basis of this?
I'm not sure if I understood what you wanted properly but I'll give it a shot. You want to add a class ("red") to the , whenever the condition is met that either the date is older than 30 days old (tex1) or the First Name is empty (tex2).
echo "<table border='1' cellpadding='10'>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
// I assume since you're just printing it out, the date is a String
// So first convert it to a timestamp
$date = strtotime($row['tex1']);
// Determine the difference, I'm doing it this way to make it easy for you to understand
$days = 60*60*24*30; // 30 days
if (empty($row['tex2']) || time()-$date > $days) {
echo '<tr>';
} else {
echo '<tr class="red">';
}
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
You should check for the conditions what you have mentioned in your question. And this can be done like this.
if(strtotime($row['tex1']) + 30 * 24 * 60 * 60 < time() && $row['tex2'] =="") {
$class="class='red'";
} else {
$class= "";
}
echo "<tr $class>";// id condition satisfies, class='red' will get echo, else echo will be null.
Try a conditional when writing the element:
echo '<tr'.
( ((strtotime($row['tex1']) < strtotime('30 days ago'))
&& (empty($row['tex2']))) ? ' class="red"' : '').
'>';

Display the result by selection each column from a html table using php

Is it possible to have filtering option in each column of html table in which the data is fetched from mysql as we do in excel sheet?
<html>
<?php
// connect to the database
include('connect-db.php');
$result = mysql_query("SELECT * FROM log WHERE type='c' ") ;
echo "<div style='height:auto;'><table border='0' cellpadding='10' class='table table-striped table-bordered'>";
echo "<tr> <th>ID</th> <th>Date</th> <th>Type</th> <th>Batch name</th> <th>No.of Pages</th> <th>User</th><th>file</th><th>Status</th><th></th><th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
$id=$row['batch_file'];
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['assdate'] . '</td>';
echo '<td>' . $row['type'] . '</td>';
echo '<td>'.substr($row['batch_file'], 0, -4).'</td>';
echo '<td>' . $row['no_pages'] . '</td>';
echo '<td>' . $row['assign'] . '</td>';
echo '<td><a href="copy_test_uploads/'.$row['batch_file'].'"target="_blank"><img src="pdf.png" style=width:35px;></td>';
echo '<td>' . $row['status'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table></div>";
?>
</html>

The Next iteration

I'm having some display problems here.
I have a "backend.php" file where I ask for two inputs.
These inputs are processed by "Addproducts.php" file and this file redirects to backend.php.
Backend.php also shows the current records in the database.
Here's the code for backend.php
<html>
<head>
<title>Inventory - Backend</title>
</head>
<body>
<form action="addproducts.php" method="post">
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="pname"/></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="text" name="productq"/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="Add Product"/></td>
</tr>
</table>
</form>
<h2>Current Products</h2>
<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$query = 'SELECT * FROM PRODUCTS';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '<table>';
echo '<tr>';
echo '<th>Product ID </th>';
echo '<th>Producr Name </th>';
echo '<th>Product Stock </th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
echo '<br/>';
echo '</table>';
}
else
{
echo "No products in the database";
}
}
?>
</body>
</html>
It displays something like this :-
Product ID Producr Name Product Stock
1 NewProduct 1
2HTC One5
3Samsung10
4Sony10
You see?
Only the first product is aligned, the rest are not.
How do I make them all align ?
Thanks.
The reason is you are closing your table tag within the loop, move it outside the loop like follows:
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
else
{
echo "No products in the database";
}
}
echo '<br/>';
echo '</table>';
Update: A better fix (see Barmar's comment below):
if (empty(mysql_num_row($result))) {
echo "<tr><td colspan='3'>No products in the database</td></tr>";
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
}
echo '</table>';
Also start looking into using mysqli(http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), mysql_ is deprecated!
I would suggest removing the
<br/>
from within your table. I believe that putting markup like line breaks inside of table markup will break the layout.

Categories