Display data from mysql database using a table - mysql

I have table of items and I want to display all the item in a table form. Each row must contain a maximum of 3 cells. How can I do that?
$query = "SELECT * FROM newrequest";
$result = mysql_query($query);
echo "<table>
<tr>";
While($row = mysql_fetch_array($result))
{
$query_two = "SELECT * FROM tblmember WHERE customer_id = '${row['customer_id']}'";
$result_two = mysql_query($query_two);
$username = mysql_fetch_array($result_two)['username'];
echo "
<td> <p><b>Advertised by:</b> " .$username . " </p>
<img src= ".$row['location']." style= width:300px; >
<p><b>Product name:</b>" .$row['product_name'] . "</p>
<p><b>Product price:</b>" . $row['price'] . "</p>
</td>";
}
echo "</tr>
</table>";
mysql_close();
?>

<?php
echo '<table>';
$i = 1;
while($row = mysql_fetch_array($result)) {
$query_two = "SELECT * FROM tblmember WHERE customer_id = '${row['customer_id']}'";
$result_two = mysql_query($query_two);
$username = mysql_fetch_array($result_two)['username'];
$flag = false;
if($i%3 == 1) echo '<tr>';
echo '<td>
<td><img src="'.$row['location'].'" style="width:300px;"/>
<p><b>Product name:</b>' .$row['product_name'] . '</p>
<p><b>Product price:</b>' . $row['price'] . '</p></td>
</td>';
if($i%3 == 0) {
$flag = true;
echo '</tr>';
}
$i++;
}
if(!$flag) echo '</tr>';
echo '</table>';
mysql_close();
?>

Related

Echo two SQL query results on a table

I have the following code:
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
$flight = "SELECT flight_id FROM flights INNER JOIN clients ON flights.nif_id=clients.nif_id";
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$flight.'</td>';
echo '</tr>';
}
}
?>
I need to echo the result of the SELECT flight_id FROM flights INNER JOIN clients ON flights.nif_id=clients.nif_id query. But when I save the file, what I get on the page is a link with that query instead of the result.
Should I start a new $sql = with that query under the first $sql =?
Or is there other way?
I tried UNION and SELECT *, flight_id FROM flights INNER JOIN clients ON flights.nif_id=clients.nif_id FROM clients ORDER BY nif_id ASC; but then I get mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given.
You don't need two queries. Just use the joined query.
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT c.prm_nome, c.apelido, c.nmr_tlm, c.mail, c.nif_id, f.flight_id
FROM clients c
JOIN flights f ON f.nif_id = c.nif_id
ORDER BY c.nif_id ASC;";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
$flight = $row['flight_id'];
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$flight.'</td>';
echo '</tr>';
}
}
?>

mysql query - to echo groups of products with foreach in stylable divs

hope somone can help me - I have this code:
$query = "SELECT * FROM `products` WHERE `category` = 100 AND `showme` = 1 `ORDER BY `nr` ASC";`
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){
echo '<div class="'.$row->design.'">
<img src="'.$row->img.'" width="100%"><br>
<span><b>'.$row->name.'</b></span><br><span>'.$row->descr.'</span><br />
<span>'.$row->preprice.' </span><span>'.$row->price.'</span><span> '.$row->unit.'</span></div>
';
}
It displays like this: http://gartenundhof.de/gartenundhof-produkte5.php
It should look about like:http://gartenundhof.de/gartenundhof-produkte.php (which has 3 separate queries)
"SELECT * FROM 'products' ORDER BY 'category' ASC";
Try this
You can try it:
$query = "SELECT * FROM `products` WHERE `showme` = 1 `ORDER BY category,`nr` ASC";
$result = mysql_query($query);
$category = -1;
while($row = mysql_fetch_object($result)){
if($category != $row->category) {
$category = $row->category;
eho "<h2>$category</h2>";
}
echo '<div class="'.$row->design.'">
<img src="'.$row->img.'" width="100%"><br>
<span><b>'.$row->name.'</b></span><br><span>'.$row->descr.'</span><br />
<span>'.$row->preprice.' </span><span>'.$row->price.'</span><span> '.$row->unit.'</span></div>';
}
Assuming you have 2 tables Products and Categories:
$categories_query = "SELECT * FROM categories ORDER BY id ASC";
$categories = mysql_query($categories_query);
while($category = mysql_fetch_object($categories)) {
echo "Category: " . $category->name . "<br />";
$products_query = "SELECT * FROM products WHERE category_id = " . $category->id . " AND showme = 1 ORDER BY nr ASC";
while($product = mysql_fetch_object($products_query)) {
echo "Product: " . $product->name . "<br />";
}
}

Updated code for my post about mysql with foreach echo

$categories = array();
$result= mysql_query("SELECT * FROM products ORDER BY category, nr ");
while($row = mysql_fetch_assoc($result)){
$categories[$row['category']][] = $row['design'];
$categories[$row['category']][] = $row['img'];
$categories[$row['category']][] = $row['name'];
$categories[$row['category']][] = $row['descr'];
$categories[$row['category']][] = $row['preprice'];
$categories[$row['category']][] = $row['price'];
$categories[$row['category']][] = $row['unit'];
}
foreach($categories as $key => $category){
echo $key.'<br/>';//prints categories
foreach($category as $item){
//echo $item.'<br/>';//prints rows within categories
echo '<div class="'.$category[0].'">
<img src="'.$category[1].'" width="100%"><br>
<span><b>'.$category[2].'</b></span><br><span>'.$category[3].'</span><br />
<span>'.$category[4].' </span><span>'.$category[5].'</span><span> '.$category[6].'</span></div>
';
}
}
It displays like this http://gartenundhof.de/gartenundhof-produkte5.php
It should look like this:http://gartenundhof.de/gartenundhof-produkte.php

2 or 3 Checkboxes in MySQL query

I created the following SQL Query, which works fine. However, I want to add 2 other checkbox $Post's from another label. Now I'm a little bit without any idea how I can add it. Do I have to add an "AND" between the tbs queries?
Here is the code
include "db_connect.inc.php";
$sql = "SELECT * FROM profiles";
$sql .= " WHERE profilename = '". $_POST["profilename"] ."' ";
$sql .= " AND ort = '". $_POST["ort"] ."' ";
$sql .= "AND jahren = '" . $_POST["alter"] . "' ";
$tbs = array();
foreach( array( 'tb1', 'tb2', 'tb3' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`grosse` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$tbs = array();
foreach( array( 'tb4', 'tb5', 'tb6', 'tb7' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`haare` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num==0) echo "Kein Profil gefunden";
echo "<table border='1'>";
echo "<tr><td>Profile</td><td>Alter</td>";
echo "<td>Ort</td><td>Brustgrösse</td>";
echo "<td>Haarfarbe</td></tr>";
while ($dsatz = mysqli_fetch_assoc($res))
{
echo "<tr>";
echo "<td>" . $dsatz["profilename"] . "</td>";
echo "<td>" .$dsatz["jahren"] . "</td>";
echo "<td>" .$dsatz["ort"] . "</td>";
echo "<td>" .$dsatz["grosse"] . "</td>";
echo "<td>" .$dsatz["haare"] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
i could it resolve it. I had a wrong entry in the DB. The query works fine!

Print out certain rows from a table

The names im using can be changed or removed in the database at times, as it is now i have to change every name in the script when that happends. Can i print it out w/o a IF for example?
<?php
$sql = "SELECT detail,
SUM(IF(depot = 'Huvuddepån', quantity, 0)) AS Huvuddepån,
SUM(IF(depot = 'Uddevalla', quantity, 0)) AS Uddevalla,
SUM(IF(depot = 'Jönköping', quantity, 0)) AS Jönköping,
SUM(IF(depot = 'Polyeten', quantity, 0)) AS Polyeten,
SUM(IF(depot = 'Krackern', quantity, 0)) AS Krackern
FROM quantities
GROUP BY detail";
$result = $conn->query($sql);
echo "<table border='0' cellspacing='3' cellpadding='3'>";
echo "<tr>";
echo "<td width='300'>"."<b>Detalj</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Huvuddepån</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Uddevalla</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Jönköping</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Polyeten</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Krackern</b>"."</td>";
echo "</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row["detail"]."</td>";
echo "<td align='center'>".$row["Huvuddepån"]."</td>";
echo "<td align='center'>".$row["Uddevalla"]."</td>";
echo "<td align='center'>".$row["Jönköping"]."</td>";
echo "<td align='center'>".$row["Polyeten"]."</td>";
echo "<td align='center'>".$row["Krackern"]."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
echo "</table>";
?>
What about this ? See sqlfiddle
SET #COLUMNS = NULL;
SELECT GROUP_CONCAT(
DISTINCT CONCAT(
'SUM(CASE WHEN depot = "',
depot,
'" THEN quantity ELSE 0 END) AS "',
depot,
'"'
)
) INTO #COLUMNS
FROM quantities;
SET #SQL = CONCAT(
'SELECT
detail,
',#COLUMNS,'
FROM
quantities
GROUP BY
detail'
);
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
It works with the following table :
CREATE TABLE quantities(
detail INT,
depot VARCHAR(20),
quantity INT
);
INSERT INTO quantities VALUES (1,"Huvuddepån",10);
INSERT INTO quantities VALUES (2,'Uddevalla',15);
INSERT INTO quantities VALUES (2,'Jönköping',20);
INSERT INTO quantities VALUES (3,'Polyeten',10);
INSERT INTO quantities VALUES (1,'Krackern',10);
INSERT INTO quantities VALUES (4,"fo'o6",10);
NOTE : Nevertheless, it will most likely break if one of your depot contains quotes ". If your data might contain such quotes, you should consider using REPLACE to replace those by \" in order to escape them.
The php code you should be using is the following one, I escaped the " in the sql because they weren't and I believe the problem came from here :
<?php
$sql = "SET #COLUMNS = NULL;
SELECT GROUP_CONCAT(
DISTINCT CONCAT(
'SUM(CASE WHEN depot = \"',
depot,
'\" THEN quantity ELSE 0 END) AS \"',
depot,
'\"'
)
) INTO #COLUMNS
FROM quantities;
SET #SQL = CONCAT(
'SELECT
detail,
',#COLUMNS,'
FROM
quantities
GROUP BY
detail'
);
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;";
$result = $conn->query($sql);
echo "<table border='0' cellspacing='3' cellpadding='3'>";
echo "<tr>";
echo "<td width='300'>"."<b>Detalj</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Huvuddepån</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Uddevalla</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Jönköping</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Polyeten</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Krackern</b>"."</td>";
echo "</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row["detail"]."</td>";
echo "<td align='center'>".$row["Huvuddepån"]."</td>";
echo "<td align='center'>".$row["Uddevalla"]."</td>";
echo "<td align='center'>".$row["Jönköping"]."</td>";
echo "<td align='center'>".$row["Polyeten"]."</td>";
echo "<td align='center'>".$row["Krackern"]."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
echo "</table>";
?>