Displaying 2 records in a column using php - php

So I have a code
<?php
$showorder = "SELECT order_number FROM orders WHERE customer_number=522";
$orderesult = mysqli_query($con, $showorder);
$ord = mysqli_fetch_array($orderesult);
?>
in my database customer number 522 has 2 order numbers, when i tried to show the result, it only shows 1.
Here's my other code
echo "<table>";
echo "<th>Order Number</th><th>Order date</th>";
echo "<tr><td>";
echo $ord["order_number"];
echo "</td><td>";
echo $ord["order_date"];
echo "</td></tr>";

You just need to use while() here for getting all records, something like:
while($ord = mysqli_fetch_array($orderesult)){
//echo all value here
}
Also note that, if you want to print $ord["order_date"] than you must need to select column also in your query.
Otherwise, $ord will only contain order_number value.

Your SQL is missing the extra column.
Current SQL:
SELECT order_number FROM orders WHERE customer_number=522
Change to:
SELECT order_number, order_date FROM orders WHERE customer_number=522

Put mysqli_fetch_array($orderesult); in a while loop.
while($ord = mysqli_fetch_array($orderesult)) {
echo $ord["order_number"];
# code
}

Replace your code with the below code and then try again
<?php
$showorder = "SELECT order_number, order_date FROM orders WHERE customer_number=522";
$orderesult = mysqli_query($con, $showorder);
echo "<table>";
echo "<tr>";
echo "<th>Order Number</th><th>Order date</th>";
echo "</tr>";
while($ord = mysqli_fetch_array($orderesult)) {
echo "<tr>";
echo "<td>$ord['order_number']</td>";
echo "<td>$ord['order_date']</td>";
echo "</tr>";
}
echo "</table>";
?>

echo "<table>";
echo "<th>Order Number</th>";
while($ord = mysqli_fetch_array($orderesult)) {
echo "<tr><td>";
echo $ord["order_number"];
echo "</td></tr>";
}

you must use loop to show all result , and you can use echo one time .
while($ord = mysqli_fetch_array($orderesult)) {
echo "<table>
<th>Order Number</th><th>Order date</th>
<tr><td>".
$ord["order_number"]."
</td></tr>";
}

Related

Want to show checkbox for each line

Currently, in our office website, there is a userinput textbox and after inserting, results from database will be shown below. There are 4 results Lot ID, Product, EWSFLOW and Zone.Among them, only zone is different. I want to do that Lot ID, Product and EWSFlow must show at once and if that entered values have 5 different zones, Zone must shown Zone: 1,2,3,4,5. << First problem has been solved. And right now, I tried to add check boxes for each zone and checkbox must shown beside each zone. But currently, checkboxes are showing at the top. Also, count of the checkboxes must be same as Zones. lets say if the inserted value have 5 zones, it has to show 5 checkboxes besides of it (Example: Zone : [checkbox] 1).
Checkboxes are showing at top
echo "<table id='corwafer'>";
$arr = array();
while ($row = mysqli_fetch_assoc($result1)) {
$field1name = $row["lotid"];
$field2name = $row["product"];
$field3name = $row["ewsflow"];
$field4name = $row["zone"];
$key = $field1name + ":" + $field2name + ":" + $field3name;
if (!in_array($key, $arr)){
echo "<tr>";
echo "<th >Lot ID:</th>";
echo "<td >$field1name</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Product:</th>";
echo "<td>$field2name</td>";
echo "</tr>";
echo "<tr>";
echo "<th>EWSFLOW: </th>";
echo "<td>$field3name</td>";
echo "</tr>";
array_push($arr, $key);
}
echo "<tr>";
echo "<th>Zone:</th>";
echo "<input type='checkbox' name='chkzone' value='chkzone'>";
echo "<td>$field4name</td>";
echo "</tr>";
}
echo "</table>";
You can define an array and put lotid, product and ewsflow into it as merged inside the loop. Then before echoing check if it's already used before :
$arr = array();
while ($row = mysqli_fetch_assoc($result1)) {
$field1name = $row["lotid"];
$field2name = $row["product"];
$field3name = $row["ewsflow"];
$field4name = $row["zone"];
$key = $field1name + ":" + $field2name + ":" + $field3name;
if (!in_array($key, $arr)){
echo "<tr>";
echo "<th >Lot ID:</th>";
echo "<td >$field1name</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Product:</th>";
echo "<td>$field2name</td>";
echo "</tr>";
echo "<tr>";
echo "<th>EWSFLOW: </th>";
echo "<td>$field3name</td>";
echo "</tr>";
array_push($arr, $key);
}
echo "<tr>";
echo "<th>Zone:</th>";
echo "<td>$field4name</td>";
echo "</tr>";
}
You can change your query and use GROUP BY feature of MySQL. Below is the query. Ignore any spelling mistakes.
$sql = "SELECT lotid, product, ewsflow, GROUP_CONCAT(zone) FROM productdb.tbl_correlationwafer WHERE lotid = ? GROUP BY lotid, product, ewsflow ORDER BY lotid";
$pq = $mysqli->prepare($sql);
$pq->bind_param('i', $productlotid);
$pq->execute();
$result = $pq->get_result();
$data = $result->fetch_all();
GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group.
GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
You can accomplish the desired output in a much simpler fashion if you were to use group_concat in the SQL query to gather together the various zone columns into a formatted value - then the PHP really needs only process a single row in the recordset and display the desired table format.
The SQL takes advantage of a prepared statement to help mitigate SQL injection - matters not that it is an internal website IMO - always better to be secure!
$sql='SELECT
`lotid`,
`product`,
`ewsflow`,
group_concat( distinct `zone` order by `zone` asc separator ", " ) as `zone`
FROM `productdb`.`tbl_correlationwafer`
WHERE `lotid` = ?
ORDER BY `lotid`';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('s', $productlotid );
$stmt->execute();
$stmt->bind_result( $lotid, $product, $ewsflow, $zone );
$stmt->fetch();
printf('
<table id="corwafer">
<tr>
<th>Lot ID:</th>
<td>%1$s</td>
</tr>
<tr>
<th>Product:</th>
<td>%2$s</td>
</tr>
<tr>
<th>EWSFLOW:</th>
<td>%3$s</td>
</tr>
<tr>
<th>Zone:</th>
<td>%4$s</td>
</tr>
</table>',
$lotid,
$product,
$ewsflow,
$zone
);

Count from table with session id

I try to build a Notification Popup I have for this a script what seems to work
but when I add a code I am = getting the numbers 1 2 3 4 from the 4 records from this user-id
I would like to see only the total number (4)
<?php
$q=mysqli_query($conn,"select * from notice where user='".$_SESSION['user']."'");
$rr=mysqli_num_rows($q);
if(!$rr)
{
echo "<h2 style='color:red'>No any notice for You !!!</h2>";
}
else
{
?>
<?php
$i=1;
while($row=mysqli_fetch_assoc($q))
{
echo "<Tr>";
echo "<td>".$i."</td>";
echo "</Tr>";
$i++;
}
?>
</table>
<?php }?>
simply add the echo out of your loop
$i=1;
while($row=mysqli_fetch_assoc($q))
{
$i++;
}
echo "<Tr>";
echo "<td>".$i."</td>";
echo "</Tr>";
Just output the $rr variable, it contains the number of rows.
mysqli_num_rows
UPDATE, example added based on your code:
<?php
$q=mysqli_query($conn,"select * from notice where user='".$_SESSION['user']."'");
$rr=mysqli_num_rows($q);
if(!$rr)
{
echo "<h2 style='color:red'>No any notice for You !!!</h2>";
}
else
{
echo "<h2 style='color:green'>Total Notice for you: $rr</h2>";
}
?>
Use MySQL's COUNT function..
assuming your user table has a column named id.. try
$q=mysqli_query($conn,"select COUNT(`id`) as USERS from notice where....
Doing this will result in 1 result, so remove your while and just do..
$row=mysqli_fetch_assoc($q);
echo $row['USERS'];

Splitting Long php generated HTML table?

I use a MySql query to select data from my DB and then print it in the form of a HTML Table. It works perfectly, fine but sometimes the table consists of hundreds of rows and the webpage looks incredibly akward. Is there a way to split the table side by side into 2 or 3 halves.
Present Output
Desired output
PHP
<?php
....
echo "<h3>Classes attended :</h3>";
echo "<table class='dates' border='1'>";
foreach ($results as $dates) {
echo "<tr><td width='50%'>";
echo $dates->db_date;
echo "</td>";
echo "<td width='50%'>";
echo $dates->day_name;
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
What would be the best way to achieve it?
Help would be appreciated.
You can use PHP to determine in your loop if the loop index is divisible by a certain number using something like this:
echo "<h3>Classes attended :</h3>";
echo "<table class='dates' border='1'>";
$rowCount = 1;
$numRows = count($results);
$maxRows = 12;
foreach ($results as $dates) {
echo "<tr><td width='50%'>";
echo $dates->db_date;
echo "</td>";
echo "<td width='50%'>";
echo $dates->day_name;
echo "</td>";
echo "</tr>";
if($rowCount % $maxRows == 0 && $rowCount != $numRows) {
echo "</table><table class='dates' border='1'>";
}
$rowCount ++;
}
echo "</table>";
That's the basics of doing this. Basically in your loop you're testing each index to see if it's divisible by $maxRows, and if so then you're going to close your table and open a new one. You'll have to add the styling to place the tables side by side.
If you wanted to expand upon this concept you can set $maxRows to be an evaluation of $numRows. For instance if you wanted to split the items as close as possible to half in order to show just two tables, you could do... $numRows = count($results); $maxRows = round($numRows / 2);
Inspired by Robert Wade's answer:
<?php
....
echo "<h3>Classes attended :</h3>";
$i=0;
$maxRows=10;
foreach ($results as $dates) {
$a=$i/$maxRows == 0 ? "<table class='dates' border='1'>":"";
$b=$i/$maxRows == 0 ? "</table>":"";
echo $a;
echo "<tr><td width='50%'>";
echo $dates->db_date;
echo "</td>";
echo "<td width='50%'>";
echo $dates->day_name;
echo "</td>";
echo "</tr>";
echo $b;
$i++;
}
?>
At last, add some css style to the tables.
You can also use array_chunk() for splitting your results. Or instead of displaing a lot of tables next to each other you can make pagination and get only some range in your query. For example:
SELECT * FROM `clients` LIMIT 5, 10
Selects 10 rows beggining from row 5. Now, when you change your page, just change limit values.

How do i add another Table ROW at the end of my loop.

I would like to add a row, after the loop, the number of rows before the LAST row is random based on the user's orders. so for example the user ordered 10, i would like to add another row, to the list. even if the order is 3 there would still be another row at the end.
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>₱".number_format(($row['price']*$row['quantity']),2)."</td>";
echo "</tr>";
if($row['carrier']=="LBC"){
echo "<tr>";
echo "<td>Shippment Option Chosen: ".$row['carrier']."</td>";
echo "<td></td>";
echo "<td>₱250.00</td>";
echo "</tr>";
}
}
echo "</table>";
?>
Here, i made it create another row, but it creates a row after each row, i just need to add 1 row at the end of the table. Thank you :)
Hmm Why not just add a new query? I fixed your codes. and added these.
$qryx = "SELECT DISTINCT customers.serial,customers.name,customers.payment,customers.carrier,customers.tracking_no, orders.date, order_detail.productid, order_detail.quantity, order_detail.price, order_detail.orderid, inventory.prod_name
FROM customers
RIGHT JOIN orders on customers.serial=orders.serial
RIGHT JOIN order_detail on orders.serial=order_detail.orderid
LEFT JOIN inventory on order_detail.productid=inventory.prod_id
where customers.email='{$_SESSION['email']}'
AND order_detail.orderid='{$_REQUEST['orderid']}'
GROUP BY customers.carrier";
$resulty = #mysql_query($qryx);
while ($rowz=mysql_fetch_array($resulty)){
if($rowz['carrier']=="LBC"){
echo "<tr>";
echo "<td>Shippment Option Chosen: ".$rowz['carrier']."</td>";
echo "<td></td>";
echo "<td>₱250.00</td>";
echo "</tr>";
}
}
I changed the name of the qry, result, and row. and added DISTINCT and GROUP BY to remove redundancy.
You should add a line right before your
echo "</table>";
line, but after the closing bracket before it. That last closing bracket is where your loop ends. It should look like this:
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>₱".number_format(($row['price']*$row['quantity']),2)."</td>";
echo "</tr>";
if($row['carrier']=="LBC"){
echo "<tr>";
echo "<td>Shippment Option Chosen: ".$row['carrier']."</td>";
echo "<td></td>";
echo "<td>₱250.00</td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td>LAST LINE...</td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
?>
You can use mysqli_num_rows to get the no. of rows. Then you can add a counter in the while loop. When the counter = mysqli_num_rows, you can echo the row you wanted to echo at last
Example:
$totalrows=mysqli_num_rows($result);
$counter=1;
while ($row=mysql_fetch_array($result)){
//echo the row data
if($counter==$totalrows)
{
//echo the extra row which you wanted.
}
}
echo "</table>";

MSSQL and PHP echos repeating rows for same information

I am sorry about the title as I am not sure whether my problem is in the PHP or the MSSQL query. I am pretty sure it is php.
I am repairing a PHP page that accesses two different MSSQL tables. the page worked fine until we had a system-wide change that created new department ID's and hence some new department names.
I have this almost completely fixed now except one minor thing. When you click List all departments, it Lists all the departments and each link has the correct individuals under them.
The problem is it lists a link for EACH individual in that department name for example if there are 10 people in the Business department we have ten links to Business and all have the same information under them.
I want to make it so there is only 1 link for each department. the two tables are directory (columns involved are Displayname and Department) and departments (columns involved are name and id)
Can someone please tell me where I need to change this so it only prints one link to each department? Is the problem with my SQL query or the PHP?
here is the code
function listDepts() {
$query = "SELECT directory.Lastname, directory.Firstname, directory.email,
directory.phone, directory.Office, directory.Department, departments.id,
departments.name FROM directory FULL JOIN departments ON
directory.Department=departments.id ORDER BY name";
$result = mssql_query($query);
echo "<h3>Please select a department:</h3>\n";
echo "<ul>\n";
for ($i=0; $i<mssql_num_rows($result); ) {
$info = mssql_fetch_assoc($result);
echo "<li>$info[name]</li>\n";
}
echo "</ul>\n\n";
}
Other query
function displayResults($query) {
$result = mssql_query($query);
if (mssql_num_rows($result) > 0) {
for ($i=0; $i<mssql_num_rows($result); $i++) {
$info = mssql_fetch_assoc($result);
if ($info[dept_name] != $last_dept) {
if ($i > 0) {
echo "</table>\n\n";
}
echo "<h3>$info[dept_name]</h3>\n\n";
echo "<table id=\"directory_table\">\n";
echo "<tr>\n";
echo "<th>Name</th>\n";
echo "<th>E-mail</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Office</th>\n";
echo "<th>Title</th>\n";
echo "</tr>\n";
}
if (!$info[dept_name] && $i==0) {
echo "<table id=\"directory_table\">\n";
echo "<tr>\n";
echo "<th>Name</th>\n";
echo "<th>E-mail</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Office</th>\n";
echo "<th>Title</th>\n";
echo "</tr>\n";
}
if ($i % 2 == 0) {
echo "<tr class=\"even\">\n";
} else {
echo "<tr class=\"odd\">\n";
}
echo "<td>";
echo ($info[Firstname]) ? "$info[Firstname]" . " " . "$info[Lastname]" : " ";
echo "</td>\n";
echo "<td>";
echo ($info[email]) ? "$info[email]" : " ";
echo "</td>\n";
echo "<td>";
echo ($info[phone]) ? "$info[phone]" : " ";
echo "</td>\n";
echo "<td>";
echo ($info[office]) ? "$info[office]" : " ";
echo "</td>\n";
echo "<td>";
echo ($info[title]) ? "$info[title]" : " ";
echo "</td>\n";
$last_dept = $info[dept_name];
}
echo "</table>\n\n";
} else {
echo "<p>No results found.</p>\n\n";
}
}
If you're only using these two fields:
echo "<li>$info[name]</li>\n";
Why not just make your select query:
SELECT DISTINCT
name
FROM
departments
ORDER BY
name
You're not using anything from the directory table, so you don't need to take anything from it.
If you've got multiple departments, try:
echo "<li>$info[name]</li>\n";
And then updating your department code to search for employees in the department with that name.
Department code:
$theSQL = "SELECT * FROM directory WHERE department IN (SELECT id FROM departments WHERE name='$department') ORDER BY Lastname");
In your for loop at the end of each iteration $i needs to be evaluated.
It should be like
for ($i=0; $i<mssql_num_rows($result); $i++)
Another way (generally) of looping is
$query = mssql_query('your_query');
while ($row = mssql_fetch_assoc($query)){
echo "<li>$row['name']</li>\n";
}

Categories