Lightbox not displaying images when using MySQL and PHPMyAdmin - php

I have encountered a small problem when trying to make my website more advanced. To put it simple:
when I wanted to display images in a table using Lightbox and PHP everyting worked well:
<tr>
<td class="w-25">
<a href="telewizory/telewizor1.jpeg" data-toggle="lightbox" data-lightbox="telewizor1" class="col-sm-4" >
<img src="telewizory/telewizor1.jpeg" class="img-fluid img-thumbnail" alt="Sheep">
</td>
<td>Philips 70PUS6704/12</td>
<td>3999</td>
</tr>
The first column presented the image, the second - name and the last one - price.
how it works
However, when I wanted to add MySQL and created a database in PHPMyAdmin something went wrong. I can still read the name and the price well, but images don't show, I can only see the alternative text - Sheep in this example.
New code:
<?php
$kat_id = isset($_GET['kat_id']) ? (int)$_GET['kat_id'] : 1;
$sql = 'SELECT `img`, `nazwa` , `cena`
FROM `produkty`
WHERE `kategoria_id` = ' . $kat_id .
' ORDER BY `nazwa`';
$wynik = mysqli_query($polaczenie, $sql);
if (mysqli_num_rows($wynik) > 0) {
while ($produkt = #mysqli_fetch_array($wynik)) {
echo
'<tr>
<td class="w-25">
<a href="telewizory/' . $produkt['img'] . ' data-toggle="lightbox" data-lightbox="' . substr($produkt['img'], 0, strpos($produkt['img'], ".")) . '" class="col-sm-4">
<img src="telewizory/' . $produkt['img'] . ' class="img-fluid img-thumbnail" alt="Sheep">'
. ' </td>
<td>' . $produkt['nazwa'] . '</td>'
. '<td>' . $produkt['cena'] . '</td>
</tr>'
. PHP_EOL;
}
} else {
echo 'wyników 0';
}
mysqli_close($polaczenie);
?>
how it doesn't work
I have no idea what may be the reason that this code doesn't work. I would be very grateful for any help :)

You should check that the $produkt['img'] here
<img src="telewizory/' . $produkt['img'] . ' class="img-fluid img-thumbnail" alt="Sheep">'
. ' </td>
is replaced with the expected value from the DB.

Related

Problem trying to add multiple choose to mysql

I’m facing a big problem here. My hotel company purchased a Booking system made with PHP from a South African company, their contract expired now they are now charging a lot of money to update the contract and add new functions that we need, so now all the burden is left for me… etc. So I have this table:
and I want to add a multiple type of room at each room. E.g.: Room 1, Type: Simple, Two Beds, Three Beds
Table coode:
<div class="table-responsive">
<table id="datatable-fixed-header" class="table datatable-show-all table-striped table-bordered">
<thead>
<tr>
<th>Nº</th>
<th>Floar</th>
<th>Room No.</th>
<th>Price per Night ( In <?php echo $currency_symbol; ?> )</th>
<th>Price per Week ( In <?php echo $currency_symbol; ?> )</th>
<th>Type</th>
<th>State</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if (count($room_details) > 0) {
$s_no = 1;
foreach ($room_details as $room) {
echo'<tr>
<td>' . $s_no . '</td>
<td>' . $room["property_name"] . '</th>
<td>' . $room["room_number"] . '</th>
<td class="text-right format_currency_td">' . $room["room_standard_night_rate"] . '</td>
<td class="text-right format_currency_td">' . $room["room_standard_weekly_rate"] . '</td>
<td>' . $room["room_type_name"] . '</td>';
if ($room["room_status"] == "AVAILABLE")
echo'<td><span class="label label-success">' . $room["room_status"] . '</span></td>';
else if ($room["room_status"] == "OCUPADO")
echo'<td><span class="label label-info">' . $room["room_status"] . '</span></td>';
else if ($room["room_status"] == "MANUNTEINCE")
echo'<td><span class="label label-warning">' . $room["room_status"] . '</span></th>';
else if ($room["room_status"] == "OUT OF SERVICE")
echo'<td><span class="label label-danger">' . $room["room_status"] . '</span></td>';
echo'<td>';
if ($room["room_status"] != "OUT OF SERVICE")
echo'<div class="th_options_kit">
<span class="th_options btn bg-teal-400 btn-icon btn-rounded btn_edit_room" data-room="' . $room["room_id"] . '" data-popup="tooltip" title="Edit Room" data-placement="left"><i class="icon-pen"></i></span>
<span class="btn bg-danger-400 btn-icon btn-rounded btn_delete_room" data-room="' . $room["room_id"] . '" data-popup="tooltip" title="Delete Room" data-placement="left"><i class="icon-bin"></i></span>
</div>';
else if ($room["room_status"] == "OUT OF SERVICE")
echo'<div class="th_options_kit">
<span class="th_options btn bg-teal-400 btn-icon btn-rounded btn_edit_room" data-room="' . $room["room_id"] . '" data-popup="tooltip" title="Room is out of service" data-placement="left"><i class="icon-pen"></i></span>
</div>';
echo'</td>
</tr>';
$s_no++;
}
}else {
}
?>
</tbody>
</table>
</div>
Php Code:
public function getRoom($where = FALSE) {
$this->db->select('room.`room_id`, '
. 'room.`property_id`, '
. 'room.`room_number`, '
. 'room.`room_type`,'
. 'room.`room_standard_night_rate`,'
. 'room.`room_standard_weekly_rate`,'
. 'room.`room_status`,'
. 'prop.`property_name`,'
. 'room_type.`room_type_name`'
);
$this->db->from('hm_room room');
$this->db->join('hm_room_type room_type', 'room.`room_type` IN (room_type.`room_type_id`)');
$this->db->join('hm_property prop', 'room.`property_id` IN (prop.`property_id`)');
if ($where !== FALSE) {
foreach ($where as $whr) {
$this->db->where_in($whr["column_name"], $whr["data"]);
}
}
$this->db->order_by("prop.`property_name`", "ASC");
$this->db->order_by("room.`room_number`", "ASC");
$query = $this->db->get();
return $query->result_array();
}
Help me solve this please.
I hope i understand your question, your table is currently just showing Room Type e.g. Simple. Is what you want it to show now "Simple, Two Bed" instead of just "Simple" ?
Then what you need to do is concatenate your fields. I can't seem to see what the database field for "Two Bed", but assuming it's "room_beds", then you need to do the below:
First step is to add the database field name to the query, assuming the name of the field in the database is called "room_beds"
$this->db->select('room.`room_id`, '
. 'room.`property_id`, '
. 'room.`room_number`, '
. 'room.`room_type`,'
. 'room.`room_standard_night_rate`,'
. 'room.`room_standard_weekly_rate`,'
. 'room.`room_status`,'
. 'prop.`property_name`,'
. 'prop.`room_beds`,' -- Add the here
. 'room_type.`room_type_name`');
Then finally you need to concatenate the room_beds field to the room_type_name that you already have as below:
<td>'. $room["room_type_name"] .' ,'. $room["room_beds"] .'</td>';

How to get rid of extra line breaks on top of table created with PHP?

I am just starting coding in PHP. I am experiencing a problem that I am not able to solve with any solution I found on the net.
I am trying to show a table with the threads that have been created on a forum. I initially created a static html code to see the result of the table. All was ok. The problem is when I create a dynamic table. I mean, using PHP language to update the table with the information in MySQL database.
When I created the table with PHP, the table appears half way down (whitespace above the table). I checked different solutions that I found in the net, but at the moment I still have the problem. While inspecting the page on Chrome, I found that there are dozens of br that I don't fully understand where they come from. In the code I am writting, there are not any br in that area, but the browser is interpreting something that causes to add lots of br (45 to be exact). I tried Firefox and the same happens.
I've read about removing capitalization or whitespaces from the code. Setting margin and padding to zero (obviously nothing worked). I have no idea what is causing the extra br.
Here you have the code. I am using html5, css3, PHP ver. 7.1.11 and MySQL.
<?php
include 'connection.php';
session_start();
$user=$_SESSION['logged_username'];
$unread_messages = $connection -> query ("SELECT unread_messages FROM userlist WHERE username ='$user'") or die ("Connection has failed.");
echo "This is the homepage." . '<br/>' . '<br/>';
echo "Session started as: " .
'<b>' . $_SESSION['logged_username'] . '</b>' .
" (" .
'Cerrar sesión'
. ")" . '<br/>';
//shows the number of unread messages
while($variable = $unread_messages->fetch_assoc())
{$_SESSION['message_amount'] = $variable['unread_messages'];}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p style="display: inline-block;">Unread messages: </p><a style="display: inline-block;" href="mensajes.php"><?php echo $_SESSION['message_amount']; ?>
</a>
<br><br>
Create new thread
<hr>
<center><div>Lastest published threads</div></center>
<?php
echo '<table style="width:100%; margin:0; padding:0; top:0; valign:top; border-collapse:collapse;">';
echo '<tr>';
echo '<th align="center" width="5%" bgcolor="#f5f5f5">Hour</th>';
echo '<th align="center" width="85%" bgcolor="#f5f5f5">Thread title</th>';
echo '<th align="center" width="10%" bgcolor="#f5f5f5">Author</th>';
echo '</tr>';
//obtain the highest index from column 'id_thread' (highest = more recent thread).
$idthreat_result=mysqli_query($connection, "SELECT MAX(id_thread) FROM threads");
$row=mysqli_fetch_array($idthreat_result);
$mostrecent_thread = $row[0]; //stores the id of the most recent thread
//shows in first place the most recent thread
for ($i=$mostrecent_thread; $i > 0 ; $i--)
{
//obtains information from database
//HOUR
$result_creationhour=mysqli_query($connection, "SELECT creation_hour FROM threads WHERE id_thread='$i'");
$row_hour=mysqli_fetch_array($result_creationhour);
//THREAD'S TITLE
$result_threadtitle=mysqli_query($connection, "SELECT title FROM threads WHERE id_thread='$i'");
$row_title=mysqli_fetch_array($result_threadtitle);
//THREAD'S AUTHOR
$result_threadauthor=mysqli_query($connection, "SELECT creator FROM threads WHERE id_thread='$i'");
$row_author=mysqli_fetch_array($result_threadauthor);
//shows the information throught the browser using <tr> + <td>
//each tr correspond to a row
echo '<tr>';
echo '<td>' . $row_hour[0] . '</td>' . '<br/>'; //hour when the thread was created
echo '<td>' . $row_title[0] . '</td>' . '<br/>'; //title of the thread
echo '<td>' . $row_author[0] . '</td>' . '<br/>'; //author of the thread
echo '</tr>';
}
echo '</table>';
?>
</body>
</html>
Just remove your <br> after the </td> in this part of code :
echo '<tr>';
echo '<td>' . $row_hour[0] . '</td>' ; //hour when the thread was created
echo '<td>' . $row_title[0] . '</td>' ; //title of the thread
echo '<td>' . $row_author[0] . '</td>' ; //author of the thread
echo '</tr>';
Because <br> is not available in <table> structure, and the browser show them before.

Show data from db into another div (from selected $rowId )

I'm having problems with showing data from a selected row into another div.
Meaning I'm showing eg forename and lastname, when the user clicks on the name (as a link), I would like to show more data from the Id chosen, and I would like it to be shown in a div box below.
See the site here: http://kristoff.it/onlinecoaching/
And my code:
<div class="greenBox1">
<h1>1 - VÆLG DIN ONLINE COACH</h1>
<div class="whiteBox1">
<?php
$sql = "SELECT * FROM coach ";
$coachId = $row["coachId"];
$fornavn = $row["fornavn"];
$efternavn = $row["efternavn"];
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result))
{
echo '<table border="0" align="left" height="100">';
echo '<tr>';
echo '<td width="95" rowspan="2" align="center" valign="middle"><img src="' . $row['imgUrl'] . '" width="85" height="85" alt="' . $row['imgAlt'] . '"/>' . '</td>';
?>
<!-- here I'm trying to write the id of the selected name to the div box below -->
<?
echo '<td><h2>' . $row['fornavn'] . $row['efternavn'] . '</h2></td>';
echo '</tr>';
echo '<tr>';
echo '<td valign="top" width="190"><p>' . $row['beskrivKort'] . '<br></p></td>';
echo '</tr>';
echo '</table>';
}
?>
</div>
</div>
<div class="greenBox2">
<h1>2 - BOOK TID I COACHENS KALENDER</h1>
<div class="whiteBox2" id="whiteBox2">
<!-- here would like the more data to show -->
</div>
</div>
Regards Maria
In your website, your URLs are empty. You need to put your ID.
echo '<td><h2>' . $row['fornavn'] . $row['efternavn'] . '</h2></td>';
With that URL, you will send the ID as a GET variable.
You must then request your data if the ID is set.
<div class="whiteBox2" id="whiteBox2">
<?php
if (isset($_GET['id']))
{
$sql = "SELECT * FROM coach WHERE coachId=" . (int)$_GET['id'];
// Do your stuff...
echo "Hello";
}
?>
<!-- here would like the more data to show -->
</div>

foreach loop not displaying desired result

Hi guys and thank you for your time. My question is regarding.
I am trying to loop over images in my folder along with a post in the database with the end result looking like this:
Post 1 Image 1
Post 2 Image 2
Post 3 Image 3
At the moment i get this result:
Post 1 Image 1
Post 1 Image 2
Post 1 Image 1
Post 2 Image 1
Post 2 Image 2
Post 2 Image 3
I do not want this result.
Below is my code:
$post_info = get_posts();
foreach ($post_info as $info){
$photos = glob('design/img/*');
foreach($photos as $photo) {
echo " <a href='feed.php?pid=".$info['post_id']." ' >
<div style='background:#FFF5C3'> <br> <h2> ".$info['person_mentioned']." </h2>
<h3 style='color: black'> ".$info['body']." </h3> </div> </a>";
echo " <img src='{$photo}' width='285px' height='200px' style='border: 5px solid black'>";
}
}
Thanks for your time.
Try this out (minus potential language specifics since I didn't actually run to check this code).. It's basically a regular for loop instead of a foreach.
$post_info = get_posts();
$photos = glob('design/img/*');
if (count($post_info) === count($photos)) { // According to your requirement, the counts would be the same
$count = count($post_info);
for ($i = 0; $i < $count; $i++) {
$info = $post_info[$i];
$photo = $photos[$i];
echo " <a href='feed.php?pid=".$info['post_id']." ' > <div style='background:#FFF5C3'> <br> <h2> ".$info['person_mentioned']." </h2>
<h3 style='color: black'> ".$info['body']." </h3> </div> </a>";
echo " <img src='{$photo}' width='285px' height='200px' style='border: 5px solid black'>";
}
}
Hope that helps :)
Getting image details from get_posts() and removing inner foreach loop may fix your problem.
Note: replace $info['something_like_post_image'] with your image field.
$post_info = get_posts();
foreach ($post_info as $info) {
//$photos = glob('design/img/*');
//foreach ($photos as $photo) {
echo " <a href='feed.php?pid=" . $info['post_id'] . " ' >
<div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2>
<h3 style='color: black'> " . $info['body'] . " </h3> </div> </a>";
echo " <img src='" . $info['something_like_post_image'] . "' width='285px' height='200px' style='border: 5px solid black'>";
//}
}
UPDATE
/*
* If your images have any naming convention like
* imageFileName = "image_{POST_ID}.jpg"
* then you can use below code (NO DATABASE ENTRY REQUIRED)
* (ie, For post #1 image file would be "image_1.jpg";
* and for post #2 image file would be "image_2.jpg")
*/
$post_info = get_posts();
foreach ($post_info as $info) {
//filename = image_1.jpg or image_2.jpg or...
$photoFileName = 'design/img/' . 'image_' . $info['post_id'] . '.jpg';
if (file_exists($photoFileName)) {
echo " <a href='feed.php?pid=" . $info['post_id'] . " ' >
<div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2>
<h3 style='color: black'> " . $info['body'] . " </h3> </div> </a>";
echo " <img src='" . $photoFileName . "' width='285px' height='200px' style='border: 5px solid black'>";
}
}
NOTE: You should have to keep a relation with each post against your unique image; otherwise you will not be able to get that unique image with your post, while listing.
Checkout below options to handle this situation.
You can keep image name in database (for each post, you can get your image name directly from database)
Use a naming convention for your images (for post #1 use unique image name (say image_1, for post #2 image_2 etc)
UPDATE - 2
If you are looking for a cycle-through images (without any condition), use below code
/*
* If you are looking for a solution that cycles each images
* along with each post, try this one
*/
$post_info = get_posts();
$photos = glob('design/img/*');
$numPhotos = count($photos) + 1;
//assuming your post# starts with 1
$imageId = 1;
foreach ($post_info as $info) {
//cycling
if ($imageId % $numPhotos === 0) {
$imageId = 1;
}
$photoFileName = 'design/img/' . 'image_' . $imageId++ . '.jpg';
//no need of this checking, since you are cycling
//if (!file_exists($photoFileName)) {
// $photoFileName = 'path/to/default/image.jpg';
//}
echo " <a href='feed.php?pid=" . $info['post_id'] . " ' >
<div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2>
<h3 style='color: black'> " . $info['body'] . " </h3> </div> </a>";
echo " <img src='" . $photoFileName . "' width='285px' height='200px' style='border: 5px solid black'>";
}

Order list/ while loop php issue

I have put together a basic order list for admin users in php for checking order contents placed by logged in users.
The aim of this script is to retrieve the order details (item, quantity, price) as well as the user’s first name and surname (where ‘Order for:’ is).
The script below does everything ok in that it retrieves the order (and orders if there are more than one) and it’s/their item, quantity and price.
However, it doesn’t display the user’s name and surname.
I know the problem is that where I am trying to display the name is outside the while loop but Im a little stuck in where it should sit. Any suggestions? Code is below:
<?php
$page_title = 'View Individual Order';
include ('includes/header.html');
// Check for a valid user ID, through GET or POST.
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
{ // Accessed through view_users.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) )
{ // Form has been submitted.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
include ('./includes/header.html');
exit();
}
?>
<h1>Order Details</h1>
<?php
require_once ('database.php'); // Connect to the db.
// Retrieve the user's, order and product information.
$query = "SELECT us.users_id, us.users_sales_id, us.users_first_name, us.users_surname, us.users_dealer_name,
ord.order_id, ord.users_id, ord.total, ord.order_date,
oc.oc_id, oc.order_id, oc.products_id, oc.quantity, oc.price,
prd.products_id, prd.products_name, prd.price
FROM users AS us, orders AS ord, order_contents AS oc, products AS prd
WHERE ord.order_id=$id
AND us.users_id = ord.users_id
AND ord.order_id = oc.order_id
AND oc.products_id = prd.products_id
";
$result = mysql_query ($query) or die(mysql_error());
if (mysql_num_rows($result)) { // Valid user ID, show the form.
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
while($row = mysql_fetch_array($result, MYSQL_NUM)) { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
}// end of WHILE loop
echo '</table>
<p> Here:</p>
<br><br>
<p> << Back to Orders</p>
<p> </p>
<p> </p>
<p> </p>
';
} else { // Not a valid user ID.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}
mysql_close(); // Close the database connection.
?>
<p>Footer here</p>
<?php
include ('./includes/footer_admin_user.html'); // Include the HTML footer.
?>
One way you could do it is grab the row first, and then use a do/while loop instead of just a basic while loop. Like this:
if (mysql_num_rows($result)) { // Valid user ID, show the form.
/*********** I added this line ***********/
$row = mysql_fetch_array($result, MYSQL_NUM);
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
/*********** I changed this from a while loop to a do-while loop ***********/
do { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
} while($row = mysql_fetch_array($result, MYSQL_NUM)); // end of WHILE loop
It looks like the problem is when you're trying to display the user's name:
echo '<p>Order for:<strong>'.$row[2].' '.$row[3]
The $row variable doesn't exist yet. Not seeing your database or the result from your database query, my guess is that the user's name is repeated next to every single item in their order, so it might be as simple as just starting the WHILE loop, and checking to see if you've printed their name yet:
$lastUser = NULL;
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
if ($row[0] !== $lastUser) {
if (isset($lastUser)) {
// finish up the report for the previous user
}
// echo the stuff for the current user's name
$lastUser = $row[0];
}
// go on echo-ing their order information
}
// after the while loop is over,
// close up the last user's report
But like I said, this is just a guess, and might be totally off.
The problem is that you tried to access $row[2] and $row[3] before mysql_fetch_array(). Since you are already echo'ing HTML tags, why don't you "buffer" your output first like this?:
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
$order = '<tr bgcolor="' . $bg . '">
<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
$orders[$row[2] . " " . $row[3]][] .= $order;
}
Then do a second foreach loop for the $orders
foreach($orders as $name => $orderList)
{
echo "Order for: $name";
echo "<table ...>";
foreach($orderList as $order)
{
echo $order;
}
echo "</table>";
}

Categories