include the url in mysql field in creating a menu - php

I want to make the generated list from mysql data clickable or url is included. Is there a way to do this? part of the field is menu_parent and menu_url.
This is the sample generated list...
Boats
Cars
Trucks
when one of the item is clicked, it will be directed to the corresponding link or url in the mysql.
this is the mysql fields
menu item | menu_url
boats | boats.html
cars | cars.html
trucks | trucks.html'
Any help is appreciated.

$sql=$dbh->prepare("SELECT * FROM table");
$sql->execute();
while($r=$sql->fetch()){
echo "<a href='".$r['menu_url']."'>".$r['menu item']."</a>";
}

I prefer Foreach Other Than While...
$result = "SELECT * FROM yourTable";
$query = $glb_connection->prepare($result);
$query->execute();
$qry = $query->fetchAll();
foreach ($qry as $menu) {
echo "<a href='".$menu['menu_url']."'>".$menu['menu item']."</a>";
}

Related

Not understanding the Join Function

Thanks in advance for any time you spend on my question.
I am trying to display data in a way that will display the manufacturer as a name instead of a number.
Basically when they store the data they choose a manufacturer from a drop down which is generated from a table.. IE Trogues = 1 so products stores the #1 so I know that any beer is associated with trogues is 1. Now I want to display the data but instead of having a 1 I would like to have Trogues be displayed. Where you see manufacturer in the echo code below..
I am not understanding the process logic here..
error_reporting(E_ALL);
ini_set('display_errors', 1);
$sql = "SELECT * FROM products
LEFT JOIN manufacturer
ON product.manufacturer = manufacturer.id
ORDER BY manufacturer.id, product.id";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "
<div class=reportclientproduct>".$row['manufacturer']." - <a href=".$row['website']." target=_blank>".$row['product']."</a></div>";
}
Have you tried the query like this:
$sql = "SELECT man.id AS manufac, products.product AS prod FROM products
LEFT JOIN manufacturer as man
ON product.manufacturer = manufacturer.id
ORDER BY manufacturer.id, product.id";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "
".$row['manufac']." - ".$row['prod']."
";
}
Assuming that the table products had a column named manufacturer which holds the ID of the manufacturer, and that both tables have columns name ID which hold the ID of the table item.
Also the JOIN functions may vary based on the database you use. But the aforementioned method is for mysql.

SELECT duplicate columns in MYSQL database PHP

I have a database table titled products.
I have 2 columns in the table titled name and image
I have 4 entries in my database
item 1 | image1.jpg
item 2 | image1.jpg
item 3 | image1.jpg
item 4 | image2.jpg
I need to display the results on a page but I only need to display image.1jpg once, my while() loop is obviously displaying all 4 images.
I've been at this for hours, please help!
My code:
$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' ORDER BY id ASC");
while($fetch = mysql_fetch_assoc($query)){
$image = $fetch['image'];
$name = $fetch['name'];
echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';
}
What you need is using GROUP BY in your query. So change the query as
SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC
Try the code below;
$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC");
while($fetch = mysql_fetch_assoc($query)){
$image = $fetch['image'];
$name = $fetch['name'];
echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';
}
You can also use a trick like not using while statement at all. If you just write the
$fetch = mysql_fetch_assoc($query) without while you would just get the first result in the query. I hope this helps.

Data display on PHP (Select Query)

I have a table which contains:
transaction_id | the_pet | name_of_the_owners
1 dog shiela
2 dog ben
3 dog alice
4 cat jonathan
and on my query:
$query="select * from table ORDER BY name_of_the_owner limit 5";
$r=mysqli_query($query);
while ($row=mysqli_fetch_array($r)) {
echo "<tr>";
echo "<td><a href='../php/ownersname.php?the_pet=".$row['the_pet']."'>".$row['the_pet']."</a></td>";
echo "</tr>";
}
However, when I use the $query, it shows all the data from the table.
What I need is this:
the_pet
dog (hyperlink)
cat (hyperlink)
So whenever I click on the hyperlink, the name_of_the owners will be shown in another page
for the dog when clicked
name_of_the_owners
shiela
ben
alice
I already used
$query = "SELECT MAX(transaction_id) as transaction_id, the_pet GROUP BY the_pet, name_of_the_owners;
but when I clicked on the hyperlink, it doesn't show the owners. :(
so if I understand it right you dont want to show duplicates right ?
I think the best way to do this, is to change your select query to
$query = "SELECT DISTINCT the_pet FROM table";
and if you dont want to do that you could filter a array for duplicates
like so:
$arr = array('php','jsp','asp','php','asp');
$unique = array_unique($arr);
print_r($dups);
both will do the trick!

How performance is affected by storing categories as comma delimited array and extracting each in a loop?

Namastey!
I'm currently working on one of my projects where I get to one doubt.
What I'm doing is:
I have got 4 tables called:
1: project_degree
2: project_department
3: project_category
4: Projects
project_degree have got four top level degrees which are: B.tech, M.tech, M.C.A. & M.B.A.
project_department have got their respective departments such as: C.S.E, I.T., E.E.E. and so on.
Now, project_category have some categories like PHP, .NET, SAP etc.
What I'm trying to do is, I'm adding a project to database called "projects" where I'm assigning it to its degree, department and category.
Structure of projects table is as follows:
id | project name | degree_id | Dept_id | Cat_id
1 | Sample project | 1 | 3 | 4,3,6,5,9
Now as you can see above, field "cat_id" have got multiple categories where each categories ID have been separated by a comma.
Now when I call a query get all projects from the table called "projects", I want to list of categories related to that particular project and my code goes like this:
$query = mysql_query("SELECT * from projects ORDER BY id DESC");
while($row = mysql_fetch_array($query)) {
$categories = $row['cat_id'];
$cat_arr = explode(',',$categories);
$cat_size = sizeof($cat_arr);
for($i=0;$i<$cat_size;$i++) {
$get_cat = mysql_query("SELECT * from project_category WHERE id='".$cat_arr['$i']."'");
$cat_row = mysql_fetch_array($get_cat);
echo $cat_row['name']; // Here finally I'm getting categories name
}
}
So, finally I'm running nested loop and two queries to get a category name. I doubt it affect the page load time when it comes to huge data.
Is there any better alternative to this situation?
Here should be work
$query = mysql_query("SELECT * from projects ORDER BY id DESC");
while($row = mysql_fetch_array($query)) {
$get_cat = mysql_query("SELECT * from project_category WHERE id IN (" . $row['cat_id'] . ")");
$cat_row = mysql_fetch_array($get_cat);
echo $cat_row['name']; // Here finally I'm getting categories name
}
But normalization is better.
It's better to add a new table, project_has_catID where you can store project.id and cat.id as two columns (project_id and cat_id). Remove the Cat_id column in the project table.
Once done, simply select the name by the next query;
$query = mysql_query("SELECT project_category.name from project_category INNER JOIN project_has_catID phc ON phc.cat_id = project_category.id ORDER BY phc.project_id DESC");
By this, you have a list of category names. You don't have to loop with multiple database communications.
You could load all category names into an associative array first, then look up category names in this array when looping through the projects.
$categoryNames = array();
$query = mysql_query("SELECT id, name FROM project_category");
while ($row = mysql_fetch_array($query))
{
$categoryNames[$row['id']] = $row['name'];
}
$query = mysql_query("SELECT * FROM projects ORDER BY id DESC");
while ($row = mysql_fetch_array($query))
{
$categoryIds = explode(',', $row['cat_id']);
foreach ($categoryIds as $cat_id)
{
$cat_name = $categoryNames[$cat_id];
//do what you want with the name here
}
}
This way, you don't have to change your database structure, or the structure of your code. And you only have to execute two queries in total, so this is by far the simplest solution.
Needless to say, normalization is always better as others have indicated (never use a column that contains multiple comma-separated values), but you probably knew that and had your reasons for setting up your database tables this way.

Grouping values from my_ MySQL to be shown in separate <div> on the basis of certain criteria

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>";
}

Categories