How do I organize data in a PHP search query? - php

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>";
?>

Related

Displaying MYSQL data using PHP & HTML

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.

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>

Execute mysql Querys with jQuery/Ajax and PHP?

I have a PHP Script which lists all Applications from a Databse in a HTML Table. In an own row I want two buttons. Accept and decline
Accept should set the status to "accepted" for the id i klicked on accept.
The Same for decline.
This should update the status in the mysql database.
My Script at the moment:
echo '<table border="1">';
echo '<tr>
<th>ID</th>
<th>Name</th>
<th>Alter</th>
<th>E-Mail</th>
<th>KD</th>
<th>Steam</th>
<th>Spiele</th>
<th>Status</th>
<th>Accept</th>
<th>Decline</th>
</tr>';
while($row = mysql_fetch_object($ergebnis))
{
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->age . '</td>';
echo '<td>' . $row->mail . '</td>';
echo '<td>' . $row->kd . '</td>';
echo '<td>' . $row->steam . '</td>';
echo '<td>' . $row->spiele . '</td>';
echo '<td>' . $row->status . '</td>';
echo '</tr>';
}
echo '</table>';
I want my script to be dynamic, so I don't have to refresh the page after every change. Is it possible to do this with jQuery or Ajax? If yes, how do I do it?
change <tr> to <tr class="clickable" data-id="' . $row->id . '">
attach an event handler to tr.clickable
$('tr.clickable').on('click', function(element) {
$.get('changeStatus.php?' + $(element).data('id'));
});
improve the code above

Dynamically Apend data in html table from mysql using php

I have mysql table with following columns :
no, name, oname, quantity
I have n number of records in the tables ,I want to fetch these records in an html table that dynamically increases'decreases its row length according to the number of rows in mysql database. I am trying following but its not working.can anyone help me out here
<?php
include './connection.php';
$query = "select * from orderdetails";
$result = mysql_query($query);
echo '<table border="1" style="width:600px" align=center >';
echo '<tr bgcolor="lavendar">';
echo '<td width="15%">Order No.</td>';
echo '<td>Name</td>';
echo '<td>Order</td>';
echo '<td>Quantity</td>';
echo '</tr>';
echo '</table>';
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
echo '</table>';
?>
Where you are printing out your values, there are a few errors.
In this specific code block:
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
First off all, you would want to concatinate your strings, PHP uses . for that.
For example, to concatinate "World!" to "Hello, ", to print out Hello, World!, you would use the following:
echo "Hello, " . "World!";
You are also missing a variable sign ($) in your code, when printing out the rows.
In your case, you're using echo '<td>' row['no'] '</td>';,
where it should have been echo '<td>' . $row['no'] . '</td>';
To clarify, your code should look like this:
while($row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' . $row['no'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['oname'] . '</td>';
echo '<td>' . $row['quantity'] . '</td>';
echo '</tr>';
}
Relevant PHP documentation:
PHP String Concatination
Echo documentation
There's a typo-
row['no']
to
$row['no']
Similarly other variables too.

Categories