Mysql array data not display table - php

I have use MySQL Table get values on table ,that values used another table .i got array values
first table query:
$ordersdetail = "SELECT * FROM `order_item` WHERE `Order_ID`='".$orderid ."'";
$customerorder = mysql_query($ordersdetail );
$i=0;
while($rows = mysql_fetch_array($customerorder ))
{
$orderproduct= $rows['Order_product_ID'];
$orderids= $rows['Order_ID'];
$productId[$i] = $rows['Product_ID'];
$sizeId[$i] = $rows['Size_ID'];
$colorId[$i] = $rows['Color_ID'];
$quantiy = $rows['Order_Quantity'][$i];
$price = $rows['Unit_Price'][$i];
$subTotal = $rows['Sub_Total'];
$customerccode = $rows['Record_Status'];
$customerphone = $rows['Created_Time'];
$i++;
}
table column productid array values get used another table:
given:
for($i=0;$i<count($productId);$i++)
{
$product = "SELECT * FROM `product` WHERE `Product_ID`='".$productId[$i]."'";
$products = mysql_query($product);
while($rows = mysql_fetch_array($products))
{
$productnames = $rows['Product_Name'];
}
}
color id used :
for($i=0;$i<count($colorId);$i++)
{
$color = "SELECT * FROM `color` WHERE `Color_ID`='".$colorId[$i] ."'";
$cname = mysql_query($color);
while($rows = mysql_fetch_array($cname))
{
$colorname = $rows['Color_Name'];
}
}
same way used the values table
my questions that values displayed table continuely
table structure
$message .= "<table width='100%' border='1' cellspacing='0' cellpadding='1'>
<thead>
<tr style='background: #eee;'>
<th><strong>Quantity</strong> </th>
<th><strong>ProductName</strong> </th>
<th><strong>Size</strong> </th>
<th><strong>Style</strong> </th>
<th><strong>Price</strong> </th>
<th><strong>Total</strong> </th>
</tr>
";
for($i=0;$i<count($productId);$i++)
{
$message .=" <tr style='color: #c40000;'>
<th>" .$quantiy. "</th>
<th>" .$productnames. "</th>
<th>" .$sizename. "</th>
<th>" .$colorname. "</th>
<th>" . $price. "</th>
<th>" . $subTotal. "</th>
</tr>";
}
this format is correct more values not displayed only first values display multiple time

This is a hard one to answer because there there is a lot of issues going on....
First off....unless this is a private/internal application, you cant place variables directly into your query...look into a proper database class, I can recommend : http://www.imavex.com/php-pdo-wrapper-class/
Secondly, your are mixing your php code and html too much...I'm not going to go into this too much but in general you want to keep this stuff seperate...research MVC programming patterns.
Now to answer your question...The reason you are seeing the same data repeat is because you you are using only one variable that is getting overwritten each time...ie. $quantiy (which is spelled wrong fyi)...so to hack your approach, assign it to an array and then use an $i counter on it like you are doing with $productID
A better approach would be to do this stuff in the loop and avoid assign the vars in the first place...I'm assuming you have assign stuff to the message var...if you can skip that and echo it out directly that would probably be better.
$ordersdetail = "SELECT * FROM `order_item` WHERE `Order_ID`='".$orderid ."'";
$customerorder = mysql_query($ordersdetail );
$i=0;
while($rows = mysql_fetch_array($customerorder ))
$product = "SELECT * FROM `product` WHERE `Product_ID`='".$row[$i]."'";
$products = mysql_query($product);
$color = "SELECT * FROM `color` WHERE `Color_ID`='".$colorId[$i] ."'";
$cname = mysql_query($color);
Now you would create your table from the $rows data directly and when you goto the color/size stuff you could look through in there.
}
Now I'm not 100% sure it will work for you but you could probably do something similar to above with a single query using JOINS
SELECT * FROM `order_item` LEFT JOIN product ON order_item.productID = product.productID LEFT JOIN color ON order_item.colorID = color.colorID WHERE order_item.`Order_ID`='".$orderid ."'
Please pardon the syntax/naming conventions...the above command will not run properly on your system, it is only meant to give you an idea on what to do.

Related

How to remove empty field after matching data?

I have created a database and and its tables. Insertion has done successfully. Now I want to get data from two tables using left join.
The problem is some fields which are not matching are empty. I want to remove the empty fields and to show the matching fields data.
How can I do this?
My code is:
<?php
include 'connection.php';
//joining of two table column
$sql = "select * from oc4e_product left join item ON item.Description = oc4e_product.model";
$result = mysqli_query($conn, $sql);
echo "<table border = 1px>";
echo "<tr>
<th>S/No</th>
<th>Web Name</th><th>POS Name</th> <th>POS Name</th></tr>";
$i=1;
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $i . "</td>
<td>".$row['model'] ."</td>
<td>". $row['Description'] . "</td>
</tr>";
$i++;
}
}
echo "</table>";
?>
Maybe you need to modify your sql query:
//joining of two table column
$sql = "select * from oc4e_product left join item ON item.Description = oc4e_product.model where item.Description <> ''";
In this query i added filter not empty record item.Description <> ''

inside while loop data not displaying in php

Hi in the below i want to show Consultation Charges values for that i took the td.But in that td not displying anything even td also not showing.
After executing this query i want to find the no. of rows based on the rows i want to display the data.
My expected output:
Bill Particular Bill Sub Particular Doctor Date Dis. Amt.
Consultation Charges:
all the values based on no of rows.
php
<table width="100%">
<th>Bill Particular</th>
<th>Bill Sub Particular</th>
<th>Doctor</th>
<th>Date</th>
<th>Dis. Amt.</th>
<th>Charge</th>
<th>No. of Times</th>
<th>Amount</th>
</table>
<tr><th colspan=2>Consultation Charges:</th>
<?php
$div_options = array();
$sql = "SELECT ibp.ipd_bp_id, ibp.bp_id, bp.bp_name, ibp.bsp_id, bsp.bsp_name, ibp.doctor_id, ab.employee_name doctor, ibp.date date, ibp.amount charge, ibp.discount_amount discount, ibp.no_of_time, (ibp.no_of_time * ibp.amount) total_amount
FROM bill_particular_master bp
INNER JOIN ipd_bill_particular ibp ON ibp.bp_id = bp.bp_id
LEFT OUTER JOIN bill_sub_particular bsp ON bsp.bsp_id = ibp.bsp_id
LEFT OUTER JOIN address_book ab ON ab.employee_id = ibp.doctor_id
WHERE ibp.ipd_reg_no = '$ipd_no'
AND bsp.consultant =1
AND bsp.package = 0
AND bsp.admission = 0
AND bp.bp_name != 'Scan Charges'
AND bp.bp_name !='Procedure'";
$sth = $dbh->query($sql);
//$row=$dbh->fetch();
$i=1;
while($row=$sth->fetch(PDO::FETCH_ASSOC)){
$sub_arr['bp_name'] = $row['bp_name'];
$sub_arr['bsp_name'] = $row['bsp_name'];
echo "<tr>
<td>Here is the text - " . $sub_arr['bp_name'] . "</td>
<td>The ID of the text is - " .$sub_arr['bsp_name'] . "</td>";
if($i !== 0) {
echo "<td>The ID of the previous entry is - " .$sub_arr['bp_name'] . "</td>";
}
else {
echo "<td> </td>";
}
echo "</tr>";
$i = $row['bp_name'];
}
?>
</tr>
echo "<td>The ID of the previous entry is - " . $row['bp_name'] . "</td>";
You have there variable thats not even defined in your code.

Updating then displaying

Sorry if I do something wrong it is my first time using stackflow and just beginning php and mysql.
The problem I am getting is that the table updates AFTER it echos. I would like to display the updated table.
while($row = mysql_fetch_array($result)) {
$bids = $row['bids'];
$bids +=1;
mysql_query("UPDATE items SET bids = " .$bids.", cost=" . $price. " WHERE items.name =". $item );
echo "<tr>
<td>".$row['name']."</td>
<td align=\"right\">".$row['cost']."</td>
<td align=\"right\">".$row['bids']."</td>
<td align=\"right\">".$row['seller_name']."</td>
</tr>";
}//end while
You need to add one more line in between your update statement and display statement.
$new_record = mysql_fetch_array(mysql_query("SELECT * FROM items WHERE items.name =". $item"));
Then use your echo statement as :
echo "<tr><td>".$new_record ['name']."</td>
<td align=\"right\">".$new_record['cost']."</td>
<td align=\"right\">".$new_record['bids']."</td>
<td align=\"right\">".$new_record['seller_name']."</td>
</tr>";
You should select rows from the table again, if You want to show them after update. In the code snippet above You actually echos old entries from the table.
Just wrap it in an if block
if(mysql_query("UPDATE items SET bids = " .$bids.", cost=" . $price. " WHERE items.name =". $item )){
echo "<td>".$new_record ['name']."</td>
<td align=\"right\">".$new_record['cost']."</td>
<td align=\"right\">".$new_record['bids']."</td>
<td align=\"right\">".$new_record['seller_name']."</td>
</tr>";
}

how to link a customer and a project to oneanother?

I'm kind of new to PHP/MySQl so I would like some guidelines on how these thing work, hard to google this...
So, im trying to learn php/mysql and I'm about to write a small page with customers and each customer could have a few projects.
so, my db is set up as follows (guess this would need heavy modifications):
- customers (id, name, description)
- projects (id, name, description)
- users (id, name )
<?php
if(isset($_GET['id'])) {
$query = "SELECT * FROM customers WHERE id = '". $_GET['id']."'";
$results = mysql_query($query);
while($row = mysql_fetch_array( $results ))
{
echo "<h3>" . $row['name'] . "</h3>";
echo "<br />";
$query = "SELECT * FROM projects ORDER by name ASC";
$results = mysql_query($query);
echo "<table>
<tbody>";
while($row = mysql_fetch_array( $results ))
{
echo "<tr>
<td><a href='main.php?id=" . $row['id'] . "'>" . $row['name'] . "</a></td>
<td>" . $row['description'] . "</td>
</tr>";
}
echo "</tbody>
</table>";
}
} else {
$query = "SELECT * FROM customers ORDER by name ASC";
$results = mysql_query($query);
echo "<table>
<thead>
<tr>
<th width='20%'>Customer</th>
<th width='80%'>Description</th>
</tr>
</thead>
<tbody>";
while($row = mysql_fetch_array( $results ))
{
echo "<tr>
<td><a href='main.php?id=" . $row['id'] . "'>" . $row['name'] . "</a></td>
<td>" . $row['description'] . "</td>
</tr>";
}
echo "</tbody>
</table>";
}
?>
As you can see it missing some heavy things, for instance, if I choose Customer A or B I receive the same projects, I don't know how to separate the projects and "bind" them to a certain customer. And my intention is to "bind" users to projects and customers as well.
Any hints into the correct direction is appreciated!
You can have an INNER JOIN in the select query, like so:
"SELECT * FROM customers c INNER JOIN projects p ON p.name = c.name WHERE c.id = '". $_GET['id']."'";
Here, I have considered that you DO HAVE a name column in customer table which is the same as that in project.
You would need to add some tables.
First a table that represents the relation from Customers to Projects. This would be like
Customer ID | Project ID
1 | 1
2 | 3
....
Same would be needed for Customer and User.
Now you can reference to this table to get your needed information and perform the necessary joins to get your data.
You need another table which has the key (customers.id,projects.id) In this way you link each customer to their own projects, and you'd need to select the data from that table depending on the id of the customer. Probably you'll need a customer/user table too, to link user id to customer id.
Edit for clarity: The new table should look something like:
CREATE TABLE customer_projects (
customer_id INT NOT NULL,
project_id INT NOT NULL,
PRIMARY KEY (customer_id, project_id));
Then you assign each customer to their projects in this table. To get the details (customer name, project name), you'd need to do a "join" with other tables, something like:
SELECT customers.*, projects.* FROM customer_projects
LEFT JOIN customers ON customers.id = customer_projects.customer_id
LEFT JOIN projects ON projects.id = customer_projects.project_id
WHERE customer_projects.customer_id = '1' // 1 is an example of a customer id.

Retrieving values from several tables in a MySQL database

I have a MySQL database called "bookfeather" with several tables that contain list books. Under each table, each book has a given number of votes. The PHP code below allows the user to enter in a book title ($entry), and then returns the total number of votes that book has in all tables ($sum).
How could I use PHP to make a 2-column, 25-row table that lists the 25 books in the database with the highest value for $sum (in descending order)?
Thanks in advance,
John
mysql_connect("mysqlv10", "username", "password") or die(mysql_error());
mysql_select_db("bookfeather") or die(mysql_error());
// We preform a bit of filtering
$entry = strip_tags($entry);
$entry = trim ($entry);
$entry = mysql_real_escape_string($entry);
$result = mysql_query("SHOW TABLES FROM bookfeather")
or die(mysql_error());
$table_list = array();
while(list($table)= mysql_fetch_row($result))
{
$sqlA = "SELECT COUNT(*) FROM `$table` WHERE `site` LIKE '$entry'";
$resA = mysql_query($sqlA) or die("$sqlA:".mysql_error());
list($isThere) = mysql_fetch_row($resA);
$isThere = intval($isThere);
if ($isThere)
{
$table_list[] = $table;
}
}
//$r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC");
if(mysql_num_rows($resA)>0){
foreach ($table_list as $table) {
$sql = "SELECT votes_up FROM `$table` WHERE `site` LIKE '$entry'";
$sql1 = mysql_query($sql) or die("$sql:".mysql_error());
while ($row = mysql_fetch_assoc($sql1)) {
$votes[$table] = $row['votes_up'];
$sum += $row['votes_up'];
//echo $table . ': "' . $row['votes_up'] . " for $entry from $table\"<br />";
}
}
}
else{
print "<p class=\"topic2\">the book \"$entry\" has not been added to any category</p>\n";
}
//within your loop over the DB rows
//$votes[$table] = $row['votes_up'];
//afterwards
if($sum>0){
print "<table class=\"navbarb\">\n";
print "<tr>";
print "<td class='sitenameb'>".'<a type="amzn" category="books" class="links2b">'.$entry.'</a>'."</td>";
print "</tr>\n";
print "</table>\n";
//echo "<p class=\"topic3\">".''.$entry.''. "</p>\n";
echo "<p class=\"topic4\">". number_format($sum) . ' votes in total.'."</p>\n";
Try something like this. All of this hasn't been tested so please add comments for changes. I'll work with you to get the code right.
// After getting your array of tables formated like
$tableArray = array("`tableA`", "`tableB`", "`tableC`");
// create a table statement
$tableStatement = implode(", ", $tableArray);
// create a join statement
$joinStatement = "";
for ($i = 1; $i < count($tableArray); $i++) {
if ($joinStatement != "")
$joinStatement .= " AND ";
$joinStatement .= $tableArray[0] . ".site = " . $tableArray[$i] . ".site"
}
$firstTable = $tableArray[0];
$sql = "SELECT SUM(votes_up) FROM " . $tableStatement . " WHERE " . $joinStatement . " AND " . $firstTable . ".site LIKE '" . $entry . "' GROUP BY " . $firstTable . ".site ORDER BY SUM(votes_up) DESC";
Edit --------
I now realize that the query above won't work perfectly because votes_up will be ambiguous. Also because you probably want to be doing joins that grab records that are only in one table. I think the concept is the right direction even though the query may not be perfect.
You can do something like
$selectStatement = "SUM(tableA.votes_up) + SUM(tableB.votes_up) as total_votes_up"
I did something like this recently. In your database, you'll have to rename each field to a corresponding book name.php like (TaleofTwoCities.php). Now on your page that will display the vote results, you'll need to include some php files that will drive the database query on each load. I called mine "engine1.php" and "engine2.php." These will do all your sorting for you.
$query1 = mysql_fetch_row(mysql_query("SELECT url FROM pages ORDER BY counter DESC
LIMIT 0,1"));
$query2 = mysql_fetch_row(mysql_query("SELECT url FROM pages ORDER BY counter DESC
LIMIT 1,1"));
$query3 = mysql_fetch_row(mysql_query("SELECT url FROM pages ORDER BY counter DESC
LIMIT 2,1"));
and so on.. then..
$num1 = "$query1[0]";
$num2 = "$query2[0]";
$num3 = "$query3[0]";
That part sorts your listings by the number of votes from highest to lowest, with url, in your case, being the name of the books(remember you want it to end in .php - you'll see why in a second), and counter being the field that logs your votes.
Make your second engine.php file and add something like this:
$vquery1 = mysql_fetch_row(mysql_query("SELECT counter FROM pages WHERE
url='book1.php'"));
$vquery2 = mysql_fetch_row(mysql_query("SELECT counter FROM pages WHERE
url='book2.php'"));
$vnum1 = "$vquery1[0]";
$vnum2 = "$vquery2[0]";
and so on... Until you get to 25 for both this and engine 1.
Now, in your results page, after you put in the require_once(engine.php) and require_once(engine2.php) at the start of your body, start an HTML table. You only want two columns, so it'll be something like..
<table border=1 cellspacing=0 cellpadding=0>
<tr>
<?php include $num1; ?>
</tr>
<tr>
<?php include $num2; ?>
</tr>
And so on... By naming your field with "book1.php" and including the engines, $num1 will change to a different .php file depending on votes from high to low. Now all you have to do is make small php files for each book like so - no headers or anything because you're inserting it into the middle of html code already:
<td style="width:650px;"><center><img src="images/book1.jpg" alt="" border="none"
/></a></center></td>
<td style="width:150px;">Votes: <?php echo $vnum1;?></td>
And there you have it. A code that will dynamically give you results from high to low depending on the number of votes each book has.

Categories