SQL formatting table data - php

I have a select query that pulls all the data from the database but it displays it like so:
First name: Jasmine
Last name: Santiago
Address: 123 Butt street
City: Butt hill
Customer ID: 12
I'm trying to get it to display like this:
First name: Last name: Address: City: Customer ID:
Jasmine Santiago 123 Butt street Butt hill 12
with more rows here of just the data not the labels
But I have more than one row of data...So I'm not sure where i'm going wrong. Here's my query and echo statement.
if(isset($_POST["get-user-info"])) {
$user_info_sql = "SELECT * from customer_info";
$user_info_result = $conn->query($user_info_sql);
if ($user_info_result->num_rows > 0) {
while($row = mysqli_fetch_assoc($user_info_result)) {
echo "<tr><th>First name: </th>" . $row["First_name"]. "</tr></br>" . "<tr>Last name: " . $row["Last_name"] . "</tr></br>" . "<tr>Address: " . $row["Address"]. "</tr></br> " . "<tr>City: " . $row["City"] . "</tr></br>" . "Customer ID: " . $row["Customer_id"] . "</br>" . "</br></br>";
}
} else {
echo "No results found";
}
}
As you can see I kind of started to do a tr/th/td but i got lost...I assume maybe I need to echo out everything like....
echo table
echo tr
echo th
...but then i get lost right here because this would echo a label again...

You should display the header only once:
if ($user_info_result->num_rows > 0) {
// display table header
echo "<table><tr><th>First name: </th><th>Last name: </th><th>Address: </th><th>City: </th><th>Customer ID: </th></tr>";
while($row = mysqli_fetch_assoc($user_info_result)) {
echo "<tr><td>". $row["First_name"]. "</td>" .
"<td>". $row["Last_name"]. "</td>" .
"<td>". $row["Address"]. "</td>" .
"<td>". $row["City"]. "</td>" .
"<td>". $row["Customer_id"]. "</td></tr>";
}
echo "</table>";
}

try something like this:
<?php
if(isset($_POST["get-user-info"])) {
$user_info_sql = "SELECT * from customer_info";
$user_info_result = $conn->query($user_info_sql);
if ($user_info_result->num_rows > 0) {
echo "<tr><th>First name: </th><th>Last name:</th><th>Address:</th><th>City:</th><th>Customer ID:</th></tr>";
while($row = mysqli_fetch_assoc($user_info_result)) {
echo "<tr><td>". $row["First_name"]."</td><td>" . $row["Last_name"] . "</td><td>" . $row["Address"]. "<td></td>" . $row["City"] . "<td></td>" . $row["Customer_id"] . "</td></tr>";
}
} else {
echo "No results found";
}
}

Related

Having problems coding if and counter

I am trying to figure out how to do two things. One Query a database and get a record count (NO PROBLEM WITH THIS) Next I want to display a limited number of record on a webpage. this is where I get stuck. I am pretty sure I need to add a counter to the while loop but I keep having a problem only displaying one record 1000's of times.
if ($result->num_rows > 0) {
// output data of each row
echo "<center><table>";
echo "<tr>";
echo "<th>Legal Business Name</th>";
echo "<th>DBA Name</th>";
echo "<th>Business Address</th>";
echo "<th>Website Address</th>";
echo "<th>Business Government POC</th>";
echo "</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["LEGAL_BUSINESS_NAME"]. "</td>";
echo "<td>" . $row["DBA_NAME"]. "</td>";
echo "<td>" . $row["PHYSICAL_ADDRESS_LINE_1"]. "<br>" . $row["PHYSICAL_ADDRESS_LINE_2"]. "<br>" . $row["PHYSICAL_ADDRESS_CITY"]. ", " . $row["PHYSICAL_ADDRESS_PROVINCE_OR_STATE"]. " " . $row["PHYSICAL_ADDRESS_ZIP_POSTAL_CODE"]. "+" . $row["PHYSICAL_ADDRESS_ZIP_CODE_PLUS_4"]. " " . $row["PHYSICAL_ADDRESS_COUNTRY_CODE"]. "</td>";
echo "<td>" . $row["CORPORATE_URL"]. "</td>";
echo "<td>" . $row["GOVT_BUS_POC_FIRST_NAME"]. " " . $row["GOVT_BUS_POC_MIDDLE_INITIAL"]. " " . $row["GOVT_BUS_POC_LAST_NAME"]. "<br> P: ". $row["GOVT_BUS_POC_US_PHONE"]. "<br> E: ". $row["GOVT_BUS_POC_EMAIL"]. "</td>";
echo "</tr>";
}
echo "</table>";
Ok I figured this out. Sometimes you need a bigger hammer!
I added in a counter and created an if statement right after the while loop.
$int = 0;
while($row = $result->fetch_assoc()) {
if ($int < 10) {
echo "<tr><td>" . $row["LEGAL_BUSINESS_NAME"]. "</td>";
echo "<td>" . $row["DBA_NAME"]. "</td>";
echo "<td>" . $row["PHYSICAL_ADDRESS_LINE_1"]. "<br>" . $row["PHYSICAL_ADDRESS_LINE_2"]. "<br>" . $row["PHYSICAL_ADDRESS_CITY"]. ", " . $row["PHYSICAL_ADDRESS_PROVINCE_OR_STATE"]. " " . $row["PHYSICAL_ADDRESS_ZIP_POSTAL_CODE"]. "+" . $row["PHYSICAL_ADDRESS_ZIP_CODE_PLUS_4"]. " " . $row["PHYSICAL_ADDRESS_COUNTRY_CODE"]. "</td>";
echo "<td>" . $row["CORPORATE_URL"]. "</td>";
echo "<td>" . $row["GOVT_BUS_POC_FIRST_NAME"]. " " . $row["GOVT_BUS_POC_MIDDLE_INITIAL"]. " " . $row["GOVT_BUS_POC_LAST_NAME"]. "<br> P: ". $row["GOVT_BUS_POC_US_PHONE"]. "<br> E: ". $row["GOVT_BUS_POC_EMAIL"]. "</td>";
echo "</tr>";
$int = $int + 1;
}
}

How to replace a 'value' on mysqli_fetch_array when printing out search results?

Sorry for the confusing title. I am as confused.
SO what I am trying to do is print results from the MySQL database I've got. I have a checkbox value as "yes" in my DB and I would like to replace this to some other word while printing out the results.
I've tried different ways but all of them break the page, because I'm new to this and have no idea what I am doing.
Here is my code so far (only put what I think is relevant):
$keyword= "";
if (isset($_POST["keyword"])) {
$keyword = ($_POST["keyword"]);
}
$results = mysqli_query($con, "SELECT * FROM pcdata WHERE name LIKE '$keyword' LIMIT 0, 25");
if (!$results) {
echo "Not found...";
} else {
echo "Found...<br>";
}
while ($row = mysqli_fetch_array($results)) {
echo "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Model: " . $row['model'] . "<br>";
echo "Operating system: " . $row['model'] . "<br>";
echo "Type of computer: " . $row['pctype'] . "<br>";
echo "Other information: " . $row['info'] . "<br>";
echo "Need help ASAP: " . $row['help'] . "<br>";
}
Why don't you try a simple if inside your while:
$myvariable='';
if($row['help']='yes'){
$myvariable='put_something_here';
}
And in your echo just do:
echo "Need help ASAP: " . $myvariable . "<br>";
Or a ternary solution:
$row['help'] == 'yes' ? 'put_something_here' : 'what_do_you_want_to_print_if_it_is_not_yes'
Try this code:
$keyword= "";
if (isset($_POST["keyword"]))
$keyword=($_POST["keyword"]);
$results=mysqli_query($con,"
SELECT *
FROM pcdata
WHERE name LIKE '$keyword' LIMIT 0,25");
if (!$results) {
echo "Not found...";
}
else {
echo "Found...<br>";
}
while ($row = mysqli_fetch_array($results))
{
echo "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Model: " . $row['model'] . "<br>";
echo "Operating system: " . $row['model'] . "<br>";
echo "Type of computer: " . $row['pctype'] . "<br>";
echo "Other information: " . $row['info'] . "<br>";
echo "Need help ASAP: ";
if ($row['help'] === 'yes'){
echo 'YES';
} else {
echo 'NO';
}
echo '<br>';
}
We check the value of $row['help'] and if it "yes" printing 'YES', if other - printing 'NO'
you can also use select statement combined with Case statement which will result as desired try following code
$keyword= "";
if (isset($_POST["keyword"]))
{
$keyword = ($_POST["keyword"]);
}
//used different variable to build query
$selectquery="SELECT id,name,model,pctype,info CASE WHEN help='yes' THEN 'Your Yes String' WHEN help='no' THEN 'Your No String' else 'nothing' END as help FROM pcdata where name like '$keyword'" ;
//passed $selectquery to mysqli_qery
$results = mysqli_query($con, $selectquery);
if (!$results) {
echo "Not found...";
} else {
echo "Found...<br>";
}
while ($row = mysqli_fetch_array($results)) {
echo "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Model: " . $row['model'] . "<br>";
echo "Operating system: " . $row['model'] . "<br>";
echo "Type of computer: " . $row['pctype'] . "<br>";
echo "Other information: " . $row['info'] . "<br>";
echo "Need help ASAP: " . $row['help'] . "<br>";
}

mysqli query. Is there any way to display error? even though my query works fine

I am trying to display some data from the database into an html table. My other query works fine, i was able to retrieve some of the other tables and displaying it to my website although this particular query does not even though if have a function mysqli_error(). I tried executing the query directly from my database (phpmyadmin) and it retrieve it just fine.
here are my codes.
Note: $p_id variable has some data which is '3'.
PHP
$p_sql = "SELECT p.package_name, p.package_price, p.package_details, p.package_categories, p.package_id FROM event_table as e inner join package as p on e.package_id = p.package_id where e.package_id = '$p_id' group by e.package_id";
$data_p = mysqli_query($conn, $p_sql) ;
$result = mysqli_fetch_assoc($data_p);
$p_amount = $result['package_price'];
while ($record_p = mysqli_fetch_array($data_p)) {
echo "<table border = 1>";
echo "<tr>";
echo "<th>" . "Package Name" . "</th>";
echo "<th>" . "Package Price" . "</th>";
echo "<th>" . "Package Details" . "</th>";
echo "<th>" . "Package Categories" . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . "<br />" . $record_p['package_name'] . "<br />" . "</td>" ;
echo "<td>" . "<br />" . $record_p['package_price'] . "<br />" . "</td>" ;
echo "<td>" . "<br />" . $record_p['package_details'] . "<br />" . "</td>" ;
echo "<td>" . "<br />" . $record_p['package_categories'] . "<br />" . "</td>" ;
echo "<tr/>";
}
echo "</table>";
if(!$data_p){
echo("Error description: " . mysqli_error($conn));
}

Making a table with "mysql_fetch_array();" not working properly

I'm trying to make a page that fetches information from a database and posts it to a webpage. I tried using the "table border" and "table-height"command, but I think Ive done it wrong. Because whenever I echo something now, it shows up above the table, even though the code is underneath the "table" code. Take a look at my code (You shouldnt care about the variables I set as they may not match each other. Do not worry about this. The only thing I want to fix is the table)
<?php
include 'connection.php';
echo "<table border='2' style='border-collapse: collapse'>";
echo "<th>ID</th><th>Name</th><th>Special</th><th>Size</th>";
while($elev = mysql_fetch_array($resultstudent)){
echo "<h4> " . $student['Student'] . $student['Room'] . " </h4>";
//echo "<h4> has rom " . $ev['Rom'] . "</h4>";
}
echo "<h1> list of rooms </h1>";
while($rom = mysql_fetch_array($resultroom)){
echo "<tr><td>" .$rom['Room ID'] . "" .$rom['Roomname'] ."" . $rom['Special'] ."</td><td>" . $rom['Size'] . ";
}
?>
Try to change this line:
echo "<th>ID</th><th>Name</th><th>Special</th><th>Size</th>";
with this:
echo "<tr><th>ID</th><th>Name</th><th>Special</th><th>Size</th></tr>";
Then don't forget to close the at the end, but I think also there is some problem with the because it's not in any table TAG
I add the lines that you need in yout code see the comments "// LINE ADDED". Try with this code:
<?php
include 'connection.php';
$queryroom = "SELECT * FROM rom";
$querystudent = "SELECT * FROM elev";
$queryspecial = "SELECT * FROM rom WHERE prosjektor = 1";
$resultroomm = mysql_query($queryRom);
$resultstudent = mysql_query($queryElev);
//$resultspecial = mysql_query($queryRomProsjektor);
echo "<table border='2' style='border-collapse: collapse'>";
echo "<thead><tr>"; // LINE ADDED
echo "<th>ID</th><th>Name</th><th>Special</th><th>Size</th>";
echo "</tr></thead>"; // LINE ADDED
echo "<tbody>"; // LINE ADDED
while($elev = mysql_fetch_array($resultstudent)){
echo "<tr><td colspan='4'>"; // LINE ADDED
echo "<h4> " . $elev['Student'] . " has room nr: " . $elev['Room'] . " </h4>";
//echo "<h4> has rom " . $ev['Rom'] . "</h4>";
echo "</td></tr>"; // LINE ADDED
}
echo "<tr><td colspan='4'>"; // LINE ADDED
echo "<h1> list of rooms </h1>";
echo "</td></tr>"; // LINE ADDED
while($rom = mysql_fetch_array($resultroom)){
echo "<tr><td>" . $rom['Room ID'] . "</td><td>" . $rom['Roomname'] ."</td><td>" . $rom['Special'] ."</td><td>" . $rom['Size'] . "</td></tr>";
}
echo "</tbody></table>"; // LINE ADDED
?>
I think that you should read this:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table

Why does my echo not work given no results found?

I am performing a routine to check if books exist in a database given a range. I want to echo if no books are found. I have the following:
$search = mysqli_query($con,$query);
while(list($book_id, $title, $authors, $description, $price) = mysqli_fetch_row($search)){
if(!empty($book_id)) {
echo "Book ID: " . $book_id . "<br/>";
echo "Book Title: " . $title . "<br/>";
echo "Authors: " . $authors . "<br/>";
echo "Description: " . $description . "<br/>";
echo "Price: " . $price . "<br/>";
echo "<br/>";
}
if(empty($book_id)){
echo "Fail";
}
}
If no books are found nothing is printed. The echo does not work? How come?
Thanks
Because if no records are returned, you won't enter in the while at all, as $book_is will contain the value false and while(false)... you know
In this situation you may use mysqli_num_rows to check if there are rows found
Your no results echo is inside the while loop which will never get called
while (list($book_id, $title, $authors, $description, $price) = mysqli_fetch_row($search)) {
if (!empty($book_id)) {
echo "Book ID: " . $book_id . "<br/>";
echo "Book Title: " . $title . "<br/>";
echo "Authors: " . $authors . "<br/>";
echo "Description: " . $description . "<br/>";
echo "Price: " . $price . "<br/>";
echo "<br/>";
}
}
if (empty($book_id)) {
echo "Fail";
}
Move it outside like the code above
It is because your while loop has not been accessed at all. Try to echo something out of the if conditions, and see if it get's displayed.

Categories