I have a table where I store some teams. I am displaying them in the page via Materialize cards tag. What I want to do is to display for every team their badge.
Displaying teams
<?php
$link = mysqli_connect("localhost", "root", "", "proiect");
$sql1 = "SELECT id_club, nume, avatar FROM club";
while ($row1 = mysqli_fetch_array($qry1)) {
echo "<div class='w3-card b'>";
echo "<a href='echipa?id=" . $row1['id_club'] . "' _target='blank' title='Vezi echipa'><img src='img/referees.jpg' alt='Person' style='width:100%'>";
echo "<div class='w3-container'>";
echo "<h4><a href='echipa?id=" . $row1['id_club'] . "' _target='blank' title='Vezi echipa'><b>" . $row1['nume'] . "</b></h4>";
echo "</div>";
echo "</div>";
}
Now it is hardcoded the image that is displayed. I want the badge team image to have the same name as the club name from the database.
If the file is stored in img folder with same name then this code will work.
<?php
$link = mysqli_connect("localhost", "root", "", "proiect");
$sql1 = "SELECT id_club, nume, avatar FROM club";
while ($row1 = mysqli_fetch_array($qry1)) {
echo "<div class='w3-card b'>";
echo "<a href='echipa?id=" . $row1['id_club'] . "' _target='blank' title='Vezi echipa'><img src='img/'.$row1['nume'].'.jpg' alt='Person' style='width:100%'>";
echo "<div class='w3-container'>";
echo "<h4><a href='echipa?id=" . $row1['id_club'] . "' _target='blank' title='Vezi echipa'><b>" . $row1['nume'] . "</b></h4>";
echo "</div>";
echo "</div>";
}
?>
Related
Php beginner here. I have php loop on page to display images (basically a gallery) and each image has a title displayed below. Those titles are links to second page where only one image is shown.
The problem is that every link has a different name (value of name is specific /ID of image).
<form action="image_link.php" method="post">
<?php>
$sql_select = "SELECT * FROM images ORDER BY data DESC";
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
echo "<div class=\"galery-1-3\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";
}
?>
I'm getting a single image displayed but no matter what title I click, I always get last image from the table. How can I catch a specific ID on image_link.php, or should I use some flexible kind of address like /image_link.php?id=34. I don't know where to start.
image_link.php contains basically the same code without the loop:
$sql_select = "SELECT * FROM images WHERE ID = $SomehowGetID";
$sql_table_selected = mysql_query($sql_select);
$table_row = mysql_fetch_array($sql_table_selected);
echo "<div class=\"galery-big\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";
Thanks in advance for help.
No need for a form
first script :
<?php
$sql_select = "SELECT * FROM images ORDER BY data DESC";
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
echo "<div class=\"galery-1-3\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption><a href=\"image_link.php?id=" . $table_row['ID']
. "\" name=\"" . $table_row['ID']
. "\">" . $table_row['tytul'] . "</a></figcaption></figure></div>";
}
?>
second script
$SomehowGetID=$_GET['id'];
$sql_select = "SELECT * FROM images WHERE ID = $SomehowGetID";
$sql_table_selected = mysql_query($sql_select);
$table_row = mysql_fetch_array($sql_table_selected);
echo "<div class=\"galery-big\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul']
. "</figcaption></figure> </div>";
I'd advice you though to use prepared statement instead of simple query constructions.
Change query to:
$sql_select = "SELECT * FROM images WHERE ID =" $_GET['id'];
echo
"<figcaption>
<a href='image_link.php?id=$table_row[ID]' name='$table_row[ID]'>"
.$table_row['tytul'] ."
</a>
</figcaption>
</figure></div>";
You need to pass the id as a parameter in your href with $table_row[ID] as value.
In image_link.php
Use:
$sql_select = "SELECT * FROM images WHERE ID = '$_GET[ID]'";
You had the right idea by saying that you want to pass the id of the image.
your code should looks like this :
<form action="image_link.php" method="post">
<?php>
$sql_select = "SELECT * FROM images ORDER BY data DESC";
$sql_table_selected = mysql_query($sql_select);
while ($table_row = mysql_fetch_array($sql_table_selected)){
echo "<div class=\"galery-1-3\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";
}
?>
image_link.php:
$sql_select = "SELECT * FROM images WHERE ID =" . mysql_real_escape_string($_GET['id']);
$sql_table_selected = mysql_query($sql_select);
$table_row = mysql_fetch_array($sql_table_selected);
echo "<div class=\"galery-big\"><figure>";
echo "<img src=\"" . $table_row['file_path'] . "\" />";
echo "<figcaption>" . $table_row['tytul'] . "</figcaption></figure></div>";
Please note that the use of mysql_* function is highly deprecated, you should use mysqli extention instead
The problem is as shown in the image I've attached, my first day learning php and sql.
I want the images to be assigned to each particular item, so first item picture be displayed above the first item described rather than all 5 of them above each description. I'm still very new to this sorry. Thank you!
My code:
<?php
$con = mysqli_connect("xxxx", "xxxx",
"xxxx", "xxxx");
$query = "SELECT * FROM MyShop WHERE ID BETWEEN '1' AND '5'";
$result = mysqli_query($con, $query);
$array = array("cp.jpeg", "BV-C.jpeg", "BV-B.jpeg", "ADIY.jpeg", "CDG.jpeg",);
while($person = mysqli_fetch_array($result)) {
foreach( $array as $image ){
echo "<img src='" . $image . "' height='200' width='200'/>";
}
echo "<center><h3>" . $person['Name'] . "</h3><center>";
echo "<center><p>" . $person['Colour'] . "</p><center>";
echo "<center><p class='ex'>" . $person['Description'] . "</p><center>";
echo "<center><p> £" . $person['Price'] . "</p><center>";
}
?>
All five images are getting displayed first because your foreach loop closes after the img tag.
So it is printing each image, and then displaying your item information. Try this and see if it fixes your issue.
$count = 0;
while($person = mysqli_fetch_array($result)) {
echo "<img src='" . $array[$count]. "' height='200' width='200'/>";
echo "<center><h3>" . $person['Name'] . "</h3><center>";
echo "<center><p>" . $person['Colour'] . "</p><center>";
echo "<center><p class='ex'>" . $person['Description'] . "</p><center>";
echo "<center><p> £" . $person['Price'] . "</p><center>";
$count++;
}
Ok i have created a forum posting page where they input the data then its stored in a table then echoed out on a different page with links but i want the links to generate a page with the post then i can allow the user to comment but i have an idea for the comments i just need help with the generation of the page. The code where the user can click...
$connect = mysql_connect("localhost", "root", "123");
$database = mysql_select_db('phplogin');
$data = "SELECT * FROM forum ORDER BY time DESC";
$result = mysql_query($data);
if($connect) {
while($row = mysql_fetch_array($result)){
echo "<div class='post'>" . "<div class='leftside'>" . "<h3 class='by'>" . $row['user'] . "</h3>" . "<h6 class='when'>" . $row['time'] . "</h6>" . "</div>" . "<div class='middle'>" . "<h3 class='questiontitle'>" . htmlspecialchars($row['title']) . "</h3>" . "<p class='description'>" . htmlspecialchars($row['description']) . "</p>" . "</div>" . "<div class='rightside'>" . "<a href='#' class='answer'>Answer it</a>" . "</div>" . "</div>";
}
forgive me I just learned php this week, so I'm not sure I'm doing this all right.
It starts out accessing the DB, and the categories table for headers; it then takes that info and creates the header, pricing, and catalog links.
Then within that while loop after it completes the first part it's supposed to run a second while loop to access the products table to list all the products with the category_id that matches the cat_id from the categories table.
When it prints out it should be
Header
Pricing PDF
Item Dimensions Image Image
Item Dimensions Image Image
Item Dimensions Image Image
etc
Header
Pricing PDF
Item Dimensions Image Image
etc....
And so far the first while loop works but the second isn't. Is there a correct way to pass the variable? Can I just not access a second table while in a while loop for the first table? I dunno...I've tried a few things, and nothing is working well
<?php
//connect to server
$con = mysql_connect('localhost','username','password');
//test connection
if (!$con)
{
die ('Could not connect: ' . mysql_error());
}
//access primary DB
mysql_select_db("main_db", $con);
//place table into variable
$categories = mysql_query("SELECT * FROM categories");
//begin table build
while($row = mysql_fetch_array($categories))
{
//set shading variable
$table_row = 0;
//set current set
$cur_set = $row['cat_id'];
//create document link and header
echo "<a name='" . $row['cat_name'] . "'><h3>" . $row['cat_title'] . "</h3></a>";
//create table and table formatting cell
echo "<table id='productTable'><tr id='tableHead'>";
//table width formattting here
echo "<td style='width:165px;'></td>";
echo "<td style='width:235px;'></td>";
echo "<td style='width:155px;'>";
//link and icons to category catalog
echo "<a href='catalog/" . $row['cat_pdf'] . ".pdf'><img src='data/pdflogo.png' alt='pdf button' /></a>";
//link and icons to category pricing sheet
echo "<a href='catalog/" . $row['cat_pricing'] . ".pdf'><img src='data/pricinglogo.png' alt='pricing button' /></a>";
//finish formatting
echo "</td></tr>";
//place table into variable
$products = mysql_query("SELECT * FROM products WHERE category_id='" . $row['cat_id'] . "'");
//begin table build
while($table = mysql_fetch_array($products));
{
//create up row
echo "<tr id='tr" . $table_row . "'>";
//create first cell
echo "<td>" . $table['prod_name'] . "</td>";
//create second cell
echo "<td>" . $table['prod_dim'] . "</td>";
//create third cell
echo "<td>";
//create third cell, first image
echo "<a href='catalog/" . $table['prod_img1'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
//create third cell, second image
echo "<a href='catalog/" . $row2['prod_img2'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
//finish formatting
echo "</td></tr>";
//cycle row
if ($table_row == 0)
{
$table_row = 1;
}
else
{
$table_row = 0;
}
//end table
echo "</table>";
}
}
//close connection
mysql_close($con);
?>
Thanks in advance
It would be more streamlined to perform an INNER JOIN on both tables
SELECT
A.cat_id,A.cat_name,A.cat_title,A.cat_pdf,A.cat_pricing,
B.prod_name,B.prod_img1,B.prod_img2
FROM categories A INNER JOIN products B ON A.cat_id = B.category_id;
You can iterate on A.cat_id
This is my proposed suggestion (braces might be off, but here is what the iteration on cat_id should look like). Please change the style for starting and stopping tags.
<?php
//connect to server
$con = mysql_connect('localhost','username','password');
//test connection
if (!$con)
{
die ('Could not connect: ' . mysql_error());
}
//access primary DB
mysql_select_db("main_db", $con);
//place table into variable
$categories = mysql_query("SELECT A.cat_id,A.cat_name,A.cat_title,A.cat_pdf,A.cat_pricing,B.prod_name,B.prod_img1,B.prod_img2 FROM categories A INNER JOIN products B ON A.cat_id = B.category_id");
$current_catid = -1;
//begin table build
while($row = mysql_fetch_array($categories))
{
if ( $current_catid != $row['cat_id'] )
{
if ( $current_catid > -1 ) { echo "</table>"; }
$current_catid != $row['cat_id']
//set shading variable
$table_row = 0;
//set current set
$cur_set = $row['cat_id'];
//create document link and header
echo "<a name='" . $row['cat_name'] . "'><h3>" . $row['cat_title'] . "</h3></a>";
//create table and table formatting cell
echo "<table id='productTable'><tr id='tableHead'>";
//table width formattting here
echo "<td style='width:165px;'></td>";
echo "<td style='width:235px;'></td>";
echo "<td style='width:155px;'>";
//link and icons to category catalog
echo "<a href='catalog/" . $row['cat_pdf'] . ".pdf'><img src='data/pdflogo.png' alt='pdf button' /></a>";
//link and icons to category pricing sheet
echo "<a href='catalog/" . $row['cat_pricing'] . ".pdf'><img src='data/pricinglogo.png' alt='pricing button' /></a>";
//finish formatting
echo "</td></tr>";
}
//create up row
echo "<tr id='tr" . $table_row . "'>";
//create first cell
echo "<td>" . $table['prod_name'] . "</td>";
//create second cell
echo "<td>" . $table['prod_dim'] . "</td>";
//create third cell
echo "<td>";
//create third cell, first image
echo "<a href='catalog/" . $table['prod_img1'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
//create third cell, second image
echo "<a href='catalog/" . $row2['prod_img2'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
//finish formatting
echo "</td></tr>";
//cycle row
if ($table_row == 0)
{
$table_row = 1;
}
else
{
$table_row = 0;
}
//end table (Fix this, might produce extra table tag)
echo "</table>";
}
//close connection
mysql_close($con);
?>
You are loading relative data, so a short answer is no, because of the overhead you will probably have if you would select them with joins.
Also, try to limit the overhead created by the * by selecting only what you want and not everything.
Also follow Truth's recommendations, for new mysql(i) calls. Once you get a hang with sql and php, you can move to other types of database calls (using object oriented code -- or activerecord), but please try to understand how mysql calls are being done first.
I am building a site that has a page where I display some products that I have on my database. The products are displayed by small divs that are next to each other. I have put inside each div a link that I want to have the information of each product. The information will all have the same structure. I thought about having an
<a href="'.$row[id].".php".'">
and have an id.php file for each of the products. But that doesn t seem to make sense.
Here it is a bit of my code
<?php
mysql_select_db("myshop",$con);
$result = mysql_query('SELECT * FROM products',$con);
while($row = mysql_fetch_array($result))
{
$myimage = '<img src="'.$row['image'].'" />';
echo "<div id='appear'>" . $myimage . '<br />' . $row['title'] .
"<br />" . "<p style='color:red;' >" . "value " . $row['price'] . "€" .
"</p>". <a href="'.$row[id].".php".'">. "details" . "<a>".
</div>";
}
mysql_close($con);
?>
Create an image.php...
<?php
mysql_select_db("myshop",$con);
id = (int)$_GET['id'];
$result = mysql_query("SELECT * FROM products WHERE id=$id",$con);
print_r($result)
?>
And instead of this:
<a href="'.$row[id].".php".'">
Do this:
<a href="image.php?id='.$row[id].'">