I have one MySQL table and it has two columns -
I want to display in my site like this -
I can easily take the table name Boy | Girl . But when i try to display the table I get like this -
I am just showing one example here. There may be 50 boys and 10 girls.. So I need help.
After displaying the heading
while($row_type = mysql_fetch_array($type))
{
$type_name = $row_type['type_name']; //Boy (or) Girl taking from another table
$type_name = Database::getInstance()->query("SELECT * FROM details WHERE type='type_name'");
echo "<tr>";
while($row_name = mysql_fetch_array($type_name))
{
echo "<td>$row_name[type_name]</td>"; //Displaying the names
}
echo "</tr>";
}
So many views but not getting any answer. Please help guys. Please catch my mistake.
Try with two queries.
Make an array for each category(Male & Female)
It Works.
$maleQuery = mysql_query("SELECT * FROM table WHERE category='male'");
$femaleQuery = mysql_query("SELECT * FROM table WHERE category='female'");
while(($row = mysql_fetch_assoc($maleQuery))) {
$males[] = $row['name'];
}
while(($row = mysql_fetch_assoc($femaleQuery))) {
$females[] = $row['name'];
}
$number_of_rows = max(sizeof($males),sizeof($females));
echo "<table border='1'>";
echo "<tr><td>Male</td><td>Female</td></tr>";
for($i=0;$i<$number_of_rows;$i++)
{
echo "<tr><td>".#$males[$i]."</td><td>".#$females[$i]."</td></tr>";
}
Try this query 100% working but set php code.
SELECT IFNULL(c.Boy,'')AS Boy,IFNULL(c.Girl,'')AS Girl FROM
(SELECT x.name,CASE WHEN (x.category = 'boy') THEN x.name END AS Boy,CASE WHEN (x.category = 'girl') THEN x.name END AS Girl FROM (SELECT * FROM tablename) X)c
Is your problem to do with the creation of the table using PHP or is it related to the SQL query itself?
If it is the creation of the table then I suggest you use two tables embedded in a single table like this. That way you can build the first table using one SQL query, then build the second table using a second query, then display the table and girls and boys will appear side by side regardless of how many entries on each side. Your code would look a bit like this:
echo "<table>
<tr>
<td>
<table>
<tr><th>Boy</th></tr>";
$table = mysql_query("SELECT Name FROM details WHERE Category = 'Boy'") or die(mysql_error());
while($row = mysql_fetch_row($table))
echo "<tr><td>".$row[0]."</td>";
echo "</table>
</td>
<td>
<table>
<tr><th>Girl</th></tr>";
$table = mysql_query("SELECT Name FROM details WHERE Category = 'Girl'") or die(mysql_error());
while($row = mysql_fetch_row($table))
echo "<tr><td>".$row[0]."</td>";
echo "</table>
</td>
</tr>
</table>";
If you need to cater for more categories, do an initial search to determine the distinct categories, then put the section of code above that creates the inner table in a loop.
eg. in pseudo code:
echo the outer table <table><tr>
SELECT DISTINCT Category FROM details
while row1=get next category
$cat = row1[0];
echo "<td>
<table>
<tr><th>".$cat."</th></tr>";
$table = mysql_query("SELECT Name FROM details WHERE Category = '".$cat."'") or die(mysql_error());
while($row = mysql_fetch_row($table))
echo "<tr><td>".$row[0]."</td>";
echo "</table>
</td>
echo the </tr></table> to close the outer table
I am working on a php project with MySQL as database. Here is the table structure
product_id (PK)
shop_id (FK)
product_name
product_desc
Product_id is the primary key and shop_id is the foreign key (shows the product belongs to which shop). Now I want to select these product and display them in a <div>. For each shop products, I want to display a different <div>. e.g if there are three products say p1,p2 and p3, and they have same shop_id (say) 1, i want them to show in a separate <div>. there will be separate <div> for each shop_id, and all products belong to that shop_id will be displayed in that <div>. How can I do this with My_SQL query or using PHP. I have used a simple SELECT query like this but with this i could not find a way to initiate different <div> for separate shop.
$q = mysql_query("SELECT * FROM table");
echo "<div>";
while($product = mysql_fetch_assoc($q)){
echo $product['product_name']."<br/>";
}
echo "</div>";
It just throw all products in a single dive, I want separate div for separate shop (which contains all products of that shop). Thanks for reading..
Simplest way is to group the products by id in a array and loop through them
$all_products = array();
while($product = mysql_fetch_assoc($q)){
$all_products[$product['shop_id']][] = $product;
}
foreach($all_products as $shop_id => $product_array){
echo "<div id='shop_".$shop_id."'>";
foreach($product_array as $product){
echo $product['product_name']."<br/>";
}
echo "</div>";
}
OR call product data fro each shop id
$q = mysql_query("SELECT * FROM shops");
while($shops = mysql_fetch_assoc($q)){
$product_q = mysql_query("SELECT * FROM product_table WHERE shop_id=$shops['id']");
echo "<div>";
while($product = mysql_fetch_assoc($product_q)){
echo $product['product_name']."<br/>";
}
echo "</div>";
}
This is my first post, so I'm just getting to know how the community works so I can be helpful too, later on...
I have an index.php and db.php for testing PDO in a very small and simple app, in the first one goes the html and the second the database connection.
index.php
<h1><?php echo $fjoin_bookname; ?></h1>
<?php foreach($join as $j): ?>
<tr>
<td>
<?php echo 'Page: '. $j['pageNumber']; ?>
</td>
<td>
<?php echo $j['pageNote']; ?>
</td>
</tr>
<?php endforeach; ?>
db.php
//get books and pages Join
$query='SELECT *
FROM books
INNER JOIN pages
ON books.bookID = pages.bookID
ORDER BY pageNumber ASC';
$join = $db->query($query);
$fjoin = $join->fetch();
$fjoin_bookname = $fjoin['bookName'];
It is an app that gets page numbers with a corresponding note from a book, this will help to keep track of several books while on different devices.
Problem:
I'm not getting the first row from the 'pages' table.
It was working fine until I inserted the fetch method
$fjoin = $join->fetch();
$fjoin_bookname = $fjoin['bookName'];
Question
Would anyone be so kind to help me work this out?
When you do a call of
$join->fetch()
it returns item from current position and move cursor to the next one.
You can fetch all elements to a var and then get fjoin_bookname from the first element:
db.php
//get books and pages Join
$query='SELECT *
FROM books
INNER JOIN pages
ON books.bookID = pages.bookID
ORDER BY pageNumber ASC';
$statement = $db->query($query);
$join = $statement->fetchAll();
$fjoin = $join[0];
$fjoin_bookname = $fjoin['bookName'];
I need to echo text from a specific ID in my table..
If i use
<?php
database_connect();
$navquery = "SELECT * from content
WHERE id = 1
ORDER by position;";
$navresult = mysql_query($navquery);
?>
it will echo the text in ID 1..
but when i try
<?php
database_connect();
$navquery = "SELECT * from content
WHERE id = 2
ORDER by position;";
$navresult = mysql_query($navquery);
?>
it wont echo the text in ID 2..
help me please?
You do not have a row on your content table with an id of 2. You can try the query below to get another row.
SELECT *
FROM content
WHERE ID<>1
LIMIT 1
I have searched high and low and cant find a similar issue to what i have.
I am a beginner so please forgive my clunky query structure.
I am trying to ( have attached screen grab below of output ):
Query the photos table to get the id based on category id and also start,limit because of pagination.
Query the photos tagged table based on the photo id i just got from the first query.
But my problem is that i cant group the tags, some photos have the same tag name. And the output just shows all the tags for each photo. I want restaurant to show only once etc...
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT tag_name FROM photoTagged WHERE file_id=".$rowFile['id']." GROUP BY tag_name";
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
while ($rowTagged = mysql_fetch_array($resultTagged)) {
$tagged = $rowTagged['tag_name'];
?>
<li><a href="#"><?php echo $tagged; ?></li>
<?php }} ?>
the above query is producing:
bar,cappucino,coffee,coffee machine,restaurant,bar,cappucino,coffee,coffee machine,restaurant,bar,coffee,restaurant,bar,coffee,coffee machine
restaurant,bar,cappucino,coffee,restaurant
what i need to show is:
bar,cappucino,coffee,coffee machine,restaurant
If anyone could help i would greatly appreciate it.
Thank you in advance.
John
My new code is
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT DISTINCT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'];
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
$rowTagged = mysql_fetch_array($resultTagged);
$tagged = $rowTagged['tag_name'];
?>
<li><a href="#"><?php echo $tagged; ?></li>
<?php } ?>
I now get this: ( So i am close arent i? )
----------
cappucino
restaurant
bar
coffee machine
restaurant
coffee
coffee
restaurant
restaurant
restaurant
coffee
coffee
restaurant
restaurant
coffee machine
restaurant
coffee
I wonder if the spaces are something? i got that from copy and paste...
Any further help would be appreciated :-)
You should first perform a join between your photos and tags table, and THEN select the distinct tags.
I believe this query will let the database do all the work for you:
SELECT DISTINCT tag_name
FROM (SELECT file_id FROM $tableName WHERE cat_id=$fileID LIMIT $start, $limit) t1
LEFT JOIN photoTagged ON t1.id = photoTagged.file_id
You can also sort the tags in the database (ORDER BY tag_name).
Haven't tried it myself, so maybe the syntax is a bit off. But the idea should work.
distinct doesnt work if you are only getting one record at a time, so put the data in a PHP array and then use array_unique, which is PHPs way to do distinct
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'];
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
$rowTagged = mysql_fetch_array($resultTagged)
$tagged[] = $rowTagged['tag_name'];
}
// Let PHP do the work.
$tagged=array_unique($tagged);
while (list(,$val) = each($tagged)) {
echo "<li><a href="#">$val</li>
}
?>
you need to do a sub-query to dodge the pagination problems with the photos. If you wish the selected tags to be a subset of the photos found in your first query, then you will need to do the following.
<?php
$queryTagged = "SELECT TAG.tag_name, count(TAG.tag_name) AS num FROM photoTagged as TAG JOIN (SELECT id FROM $tableName WHERE cat_id=$fileID LIMIT $start, $limit) as PHOTO ON (PHOTO.id = TAG.file_id) GROUP BY TAG.tag_name";
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
while ($tagged = mysql_fetch_assoc($resultTagged)) {
echo "<li id="'.$tagged['TAG.tag_name'].'"><a href="#">".$tagged['TAG.tag_name']." (".$tagged['TAG.num'].")</li>";
}
?>
This way you will have two queries, on for finding the photos, and one for finding the tags for the photos on that page. This technically takes a little longer as MySQL has to load the query into a temporary table, but it should work fine.
SELECT DISTINCT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'] ?