Posting Search results in a Table Assistance Needed - php

I would like to display the results of this search in a table, would anyone be able to tell me how I could achieve such a thing please? I got this code from a tutorial website but alas it does not tell me how to get my results in a four column table. Such as this layout.
|Picture|Name|Description|Price|
<?php
$conn = mysqli_connect("localhost", "root", "", "catalog");
if(mysqli_connect_errno()){
echo "Failed to connect: " . mysqli_connect_error();
}
error_reporting(0);
$output = '';
if(isset($_GET['q']) && $_GET['q'] !== ' '){
$searchq = $_GET['q'];
$q = mysqli_query($conn, "SELECT * FROM final_dog_catologue_full WHERE name LIKE '%$searchq%' OR brand LIKE '%$searchq%'") or die(mysqli_error());
$c = mysqli_num_rows($q);
if($c == 0){
$output = 'No search results for <b>"' . $searchq . '"</b>';
} else {
while($row = mysqli_fetch_array($q)){
$Name = $row['Name'];
$brand = $row['Brand'];
$picture = $row['Picture'];
$description = $row['description'];
$Retail_Price_With_Delievery = $row['Price'];
$output .= '<a href="' . $brand . '">
<h3>' . $brand . '</h3>
<p>'. $brand .'</p>
</a>';
}
}
} else {
header("location: ./");
}
print("$output");
mysqli_close($conn);
?>

To display the values into a table using HTML, just modify the $output variable
$output .= '<tr>
<td>' . $picture. '</td>
<td>' . $name . '</td>
<td>'. $description .'</td>
<td>'. $price .'</td>
</tr>';
You would need to add the "table" tags before and after while loop
else {
$output = '<table><tr>
<th>' . $picture. '</th>
<th>' . $name . '</th>
<th>'. $description .'</th>
<th>'. $price .'</th>
</tr>';
while($row = mysqli_fetch_array($q)){...}
$output .= '</table>';

Related

PHP display images dynamically in grid pattern

I'm new to php and I'm working with a tutorial about showing images dynamically on the page, it works fine but it shows them vertically and I would like them to be horizontal. I created a page with code to do that but I can't seem to figure out where to insert the code to get the images to show.
Thanks for any help.
Vertical output looks like this
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added ASC LIMIT 6");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="1000px" border="0" cellspacing="0" cellpadding="6" align="center">
<tr>
<td width="1000px" align="center"><img style="border:#666 0px solid;" src="images/' . $id . '.jpg" width="50%" height="50%" alt="' . $product_name . '" width="77" height="102" border="1" /></td>
<td width="83%" valign="top">' . $product_name . '<br />
$' . $price . '<br /> $' . $details . '<br />
order</td>
</tr>
</table>';
}
mysql_close();
?>
Grid Output
$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15");
$i = 0;
// Establish the output variable
$dynamiclist = '<table width="1000px" border="1" cellspacing="2" cellpadding="10" align="center">';
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$price = $row["price"];
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dynamiclist .= '<tr><td>' . $product_name . '</br>' . $details . '</br>' . $price . '</td>';
} else {
$dynamiclist .= '<td>' . $product_name . '</td>';
}
$i++;
}
$dynamiclist .= '</tr></table>';
?>
I got it figured out, Thanks for the help.
$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15");
$i = 0;
// Establish the output variable
$dynamiclist = "";
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$price = $row["price"];
if ($i % 4 == 0) { // if $i is divisible by our target number (in this case "3")
$dynamiclist .= '<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
' . $details . '<br /> $' . $price . '<br />
order</td>';
} else {
$dynamiclist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
' . $details . '<br /> $' . $price . '<br />
order</td>';
}
$i++;
}
$dynamiclist .= '</tr></table>';
?>

PHP there's only 1 row is returned then duplicates it

I was wondering if anyone can explain to me why its only returning 1 row when there are 5 different rows in mysql table.
It's only showing the 1st row in all 5 rows, because I placed it in a while loop (HTMLcode) so that it can print all the other rows not just the first one.
Image that shows the problem
THANK YOU IN ADVANCE :)
PHP CODE
$id = session_id();
if ($id == "") {
session_start();
}
if (!isset($_SESSION['username'])) {
header("Location: login.php");
}
// making the connection to the database
try {
$host = '127.0.0.1';
$dbname = 'webdev_2014376';
$user = 'root';
$pass = '';
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
} catch(PDOException $e) {echo 'Error';}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// selecting the row from the database
$sqlQuery = $DBH->prepare("SELECT * FROM users");
// running the SQL
$sqlQuery->execute();
// pulling the data into a variable
$row = $sqlQuery->fetch(PDO::FETCH_ASSOC);
// taking each individual piece
$UserID = $row['UserID'];
$username = $row['Username'];
$firstname = $row['FirstName'];
$lastname = $row['LastName'];
?>
HTML CODE
<?php
echo 'hello, ' . $_SESSION['username'];
echo ' ';
echo $_SESSION['id'];
?>
<div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th class="TableCol1"> Username </th>
<th class="TableCol2"> First Name </th>
<th class="TableCol3"> Last Name </th>
<th class="TableColOpt"> Options </th>
</tr>
</thead>
<tbody>
<?php
while ($check) {
echo '<tr>';
echo '<td class="prEach1">' . $username . '</td> ';
echo '<td class="prEach1">' . $firstname . '</td> ';
echo '<td class="prEach3">' . $lastname . '</td> ';
echo '<td class="prEach4 optlinks"> '
. 'View '
. '</td>';
echo '</tr>';
$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
}
?>
</tbody>
</table>
</div>
-------------------------------------------------------
EDIT
I FOUND THE SOLUTION
SOLUTION IMAGE
PHP CODE
<?php
$id = session_id();
if ($id == "") {
session_start();
}
if (!isset($_SESSION['username'])) {
header("Location: login.php");
}
// making the connection to the database
try {
$host = '127.0.0.1';
$dbname = 'webdev_2014376';
$user = 'root';
$pass = '';
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
} catch(PDOException $e) {echo 'Error';}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// selecting the row from the database
$sqlQuery = $DBH->prepare("SELECT * FROM users");
// running the SQL
$sqlQuery->execute();
// pulling the data into a variable
$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
?>
HTML CODE
<?php
echo 'hello, ' . $_SESSION['username'];
echo ' ';
echo $_SESSION['id'];
?>
<div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th class="TableCol1"> Username </th>
<th class="TableCol2"> First Name </th>
<th class="TableCol3"> Last Name </th>
<th class="TableColOpt"> Options </th>
</tr>
</thead>
<!-- This is the category fields on the list. -->
<tbody>
<?php
//$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
while ($check) {
echo '<tr>';
echo '<td class="prEach1">' . $check['Username'] . '</td> ';
echo '<td class="prEach1">' . $check['FirstName'] . '</td> ';
echo '<td class="prEach3">' . $check['LastName'] . '</td> ';
echo '<td class="prEach4 optlinks"> '
. 'View '
. '</td>';
echo '</tr>';
$check = $sqlQuery->fetch(PDO::FETCH_ASSOC); //THIS SHOULD BE AT THE BOTTOM JUST BEFORE THE WHILE LOOP ENDS
}
?>
</tbody>
<!-- This is the get methods of the properties, where the output of the user put in the Property form will be shown -->
</table>
</div>
I was wondering if anyone can explain to me why its only returning 1 row when there are 5 different rows in mysql table.
Look what's happening inside while loop,
while ($check) {
echo '<tr>';
echo '<td class="prEach1">' . $username . '</td> ';
echo '<td class="prEach1">' . $firstname . '</td> ';
echo '<td class="prEach3">' . $lastname . '</td> ';
echo '<td class="prEach4 optlinks"> '
. 'View '
. '</td>';
echo '</tr>';
$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
}
Initially you fetched only one row from the result set using $row = $sqlQuery->fetch(PDO::FETCH_ASSOC); and put the values in the respective variables. In the while loop you're looping through the result set but actually echoing same old variables.
Solution:
If you want to display all the result set rows(including the first row) inside your table cells, then first delete these four lines,
$row = $sqlQuery->fetch(PDO::FETCH_ASSOC);
$UserID = $row['UserID'];
$username = $row['Username'];
$firstname = $row['FirstName'];
$lastname = $row['LastName'];
And then loop through the result set like this,
// your code
<?php
while ($row = $sqlQuery->fetch(PDO::FETCH_ASSOC)){
echo '<tr>';
echo '<td class="prEach1">' . $row['Username'] . '</td> ';
echo '<td class="prEach1">' . $row['FirstName'] . '</td> ';
echo '<td class="prEach3">' . $row['LastName'] . '</td> ';
echo '<td class="prEach4 optlinks"> '
. 'View '
. '</td>';
echo '</tr>';
}
?>
// your code

Update a whole table with mysqli and php

I have created a page to list all users with their rights on the website. I used the code below to create and fill the table and it works great.
echo '<form method="post" id="detail" class="group" action="includes/setup_change.php?change=rights">';
echo '<div class="group">';
if (!empty($_GET['change'])) {
if ($_GET['change'] == 'rights') { echo '<span class="message_alert success"><span class="icon success"></span><span class="text">' . _('Your password was successfully changed') . '.</span></span>'; }
}
echo '<h2>' . _('Change') . ' ' . _('rights') . '</h2>';
// select database
mysqli_select_db( $mysqli, 'db_ccadmin' );
// check connection
if ( $mysqli->connect_errno > 0 ) {
trigger_error( _('Database connection failed') . ': ' . $mysqli->connect_error, E_USER_ERROR );
}
// sql query
$sql = "SELECT * FROM users";
$res = $mysqli->query( $sql );
if( !$res ) {
trigger_error( _('Wrong') . ' SQL: [' . $sql . ']. ' . _('Error') . ' : [' . $mysqli->error . ']' );
} else {
echo '<table id="table_sort_no_search">';
echo '<thead><tr>
<th class="username">' . _('Username') . '</th>
<th class="readonly">' . _('Read-only') . '</th>
<th class="manage">' . _('Manage') . '</th>
<th class="admin">' . _('Admin') . '</th>
</tr></thead>';
echo '<tbody>';
// output query results
while($row = $res->fetch_assoc()) {
echo '<tr>';
echo '<td><input type="text" name="username" value="' . $row['username'] . '" readonly></td>';
echo '<td><label><input type="radio" class="rights" name="rights_' . $row['username'] . '" value="1" ' . (isset($row['rights']) ? (($row['rights'] == '1') ? 'checked="checked"' : '' ) : '') . '></label></td>';
echo '<td><label><input type="radio" class="rights" name="rights_' . $row['username'] . '" value="2" ' . (isset($row['rights']) ? (($row['rights'] == '2') ? 'checked="checked"' : '' ) : '') . '></label></td>';
echo '<td><label><input type="radio" class="rights" name="rights_' . $row['username'] . '" value="3" ' . (isset($row['rights']) ? (($row['rights'] == '3') ? 'checked="checked"' : '' ) : '') . '></label></td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
// free results to free up some system resources
$res->free();
The trouble is updating the whole table in the database. I don't know how to do this. Is it even possible to update the whole table (as generated) at once? If yes, how?
What would be the code that I need to put in my setup_change.php?
It would be great if you could help me!
It is not clear what you want to update, but this is how you can update (example):
$mysqli->query("UPDATE birthday SET Manage = null");
Of course, this does not solve your problem, as there is infinite ways to update the whole table. What table do you want to update, what values do you want to set?

Update a variable on click

basically I've written the following but I'm wondering what the best way to get $message_id = to the $row['id'] you click on to view an inbox message. If I just statically set $message_id = to say 39 and click on the row that's $row['id'] is 39 it'll open the message.
Basically what it does is list inbox messages I have, and anchor tag them with its row id to be used in the header to show the full contents of that message.
<p>
<?php
$message_id = "39";
if (isset($_GET[$message_id]) && empty($_GET[$message_id])) {
$username = $user_data['username'];
$get_message = mysql_query("SELECT * FROM `users_inbox` WHERE `id` = '$message_id' AND `send_to` = '$username'");
$get_message_result = mysql_fetch_array($get_message);
echo '<h1><u>' . $get_message_result['subject'] . '</u></h1>';
echo $get_message_result['sent_from'] . '<br />';
echo $get_message_result['body'] . '<br />';
echo 'Back';
}
$username = $user_data['username'];
$result = mysql_query("SELECT * FROM `users_inbox` WHERE `send_to` = '$username'");
?>
<table border="1">
<tr>
<td>From</td>
<td>Subject</td>
<td>Date</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
echo
'<tr>' .
'<td>' . $row['sent_from'] . '</td>' .
'<td>' . $row['subject'] . '</td>' .
'<td>' . $row['post_date'] . '</td>' .
'</tr>';
}
?>
</table>
</p>
Anyone know if this is even possible, as I know code is read from top to bottom and as far as I'm aware you cant go back upwards at all.
I assume that the $user_data['username'] is set or available,if yes then you could pass a variable name of you choice which points the message id instead of sending only the id (eg: i am setting it as msg_id ), Now you can access the message id via $_GET['msg_id']
<p>
<?php
$username = $user_data['username'];
if ( isset( $_GET['msg_id'] ) ) {
$get_message = mysql_query("SELECT * FROM `users_inbox` WHERE `id` = '$message_id' AND `send_to` = '$username'");
$get_message_result = mysql_fetch_array($get_message);
echo '<h1><u>' . $get_message_result['subject'] . '</u></h1>';
echo $get_message_result['sent_from'] . '<br />';
echo $get_message_result['body'] . '<br />';
echo 'Back';
}else{
$result = mysql_query("SELECT * FROM `users_inbox` WHERE `send_to` = '$username'");
?>
<table border="1">
<tr>
<td>From</td>
<td>Subject</td>
<td>Date</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
echo
'<tr>' .
'<td>' . $row['sent_from'] . '</td>' .
'<td>' . $row['subject'] . '</td>' .
'<td>' . $row['post_date'] . '</td>' .
'</tr>';
}
?>
</table>
</p>

How do I display / filter search results with php / mysql

I'm very new to working with php / mysql coding, and I've got a slight problem with displaying results that are linked to an sql database.
At the moment people search using the below code - which works fine - it's a drop down select box, but you can only select one option. I want it so the items are check boxes and you can select more than one item.
I've included below the code that's used for when people input the data to the database - which are check boxes - and I have tried replacing the 'drop down select code' with this but it doesn't work.
Does anybody know what code I have to use to replace the 'drop down select code' so that checkboxes are viewable and you can filter more than one item - I have also included the 'results page code', which displays the results and I'm thinking that 'ClientStage' needs adding to the 'check box code' somewhere.
Sorry about my lack of knowledge with this and would be grateful for some help?
DROP DOWN SELECT CODE
<select name="ClientStage" id="ClientStage">
<option value=""></option>
<?php
include 'Easyspace.php';
$sql = 'SELECT * FROM `clienttype`;';
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$ClienttypeID = $row["ClienttypeID"];
$Clienttype = $row["Clienttype"];
echo '<option value="' .$ClienttypeID. '">' .$Clienttype. '</option>';
}
?>
</select></span>
CHECK BOX CODE
<table width="100%" border="0" cellspacing="1">
<?php
$side=1;
$sql = 'SELECT * FROM `clienttype`;';
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$ClienttypeID = $row["ClienttypeID"];
$Clienttype = $row["Clienttype"];
if ($side == 1){
$side = 2;
echo '<tr>';
echo '<td><span class="RPATtext"><input type="checkbox" name="Clients[]" value="' .$ClienttypeID. '"> ' .$Clienttype. '</div></td>';
} else {
$side = 1;
echo '<td><span class="RPATtext"><input type="checkbox" name="Clients[]" value="' .$ClienttypeID. '"> ' .$Clienttype. '</div></td>';
echo '<option value="' .$ClienttypeID. '">' .$Clienttype. '</option>';
}
}
?>
</table>
RESULTS PAGE CODE
<?php
$Country = $_POST['Country'];
$County = $_POST['County'];
$ClientStage = $_POST['ClientStage'];
$HealthIssues = $_POST['HealthIssues'];
include 'Easyspace.php';
$sql = "SELECT * FROM `therapists` WHERE ";
if ($Country){
$sql .= "`Country` = '$Country'";
}
if ($County){
if ($Country){
$sql .= ' AND ';
}
$sql .= "`County` = '$County'";
}
if ($ClientStage){
if ($Country or $County){
$sql .= ' AND ';
}
$sql .= "FIND_IN_SET('$ClientStage', Client_typeID)";
}
if ($HealthIssues){
if ($Country or $County or $ClientStage){
$sql .= ' AND ';
}
$sql .= "FIND_IN_SET('$HealthIssues', IssuesID)";
}
// echo $sql;
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$TherapistID = $row['TherapistID'];
$Title = $row['Title'];
$FirstName = $row['First Name'];
$LastName = $row['Last Name'];
$PostCode = $row['PostCode'];
echo '<tr>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$Title. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$FirstName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$LastName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$PostCode. '</td>';
echo '<td height="34" valign="middle" bgcolor="#D9E5C3"><p class="bodycopy">View more details</p></td>';
echo '</tr>';
}
?>
Here's your new results code. I hope it's what you were looking for. Pay attention to your code: try to puts vars in lowercase ($Country has to be $country, try to use PDO for MySQL, etc...).
<?php
$Country = $_POST['Country'];
$County = $_POST['County'];
/* change here */
$ClientStage = $_POST['Clients'];
$HealthIssues = $_POST['HealthIssues'];
include 'Easyspace.php';
$sql = "SELECT * FROM `therapists` WHERE ";
if ($Country){
$sql .= "`Country` = '$Country'";
}
if ($County){
if ($Country){
$sql .= ' AND ';
}
$sql .= "`County` = '$County'";
}
if ($ClientStage){
if ($Country or $County){
$sql .= ' AND ';
}
/* change here */
$sql .= 'Client_typeID IN (';
foreach($ClientStage as $i => $id) {
if ($i !== 0)
$sql .= ',';
$sql .= "'" . $id . '"';
}
$sql .= ')';
}
if ($HealthIssues){
if ($Country or $County or $ClientStage){
$sql .= ' AND ';
}
$sql .= "FIND_IN_SET('$HealthIssues', IssuesID)";
}
// echo $sql;
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$TherapistID = $row['TherapistID'];
$Title = $row['Title'];
$FirstName = $row['First Name'];
$LastName = $row['Last Name'];
$PostCode = $row['PostCode'];
echo '<tr>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$Title. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$FirstName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$LastName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$PostCode. '</td>';
echo '<td height="34" valign="middle" bgcolor="#D9E5C3"><p class="bodycopy">View more details</p></td>';
echo '</tr>';
}
?>

Categories