php - show sql value with line break on retrieve - php

i'm using the following code to show clickable pictures, but underneath the picture it shows a description for the picture, in this part:
<br>".$row['optie'];"
but some lines in the table are too long to display the picture and description correctly next to each other.
So how can i show these descriptions and add a line break?
<?php
$query = "SELECT * FROM $tabel";
$result = mysql_query($query);
$aantal = 0;
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
$src = $row['afbeelding'];
echo '<div class="Image">';
echo "<td align='center'><h2><a href='pagina3.php?lang=" . $_SESSION['lang'] . "&naam=" . $naam . "&postcodehuisnummer=" .$postcodehuisnummer ."&fietskeuze=" . $fietskeuze . "&opties=" . $row['optie'] . "&optieid=" . $row['opties_id'] . "' '><img src=".$src." width='400px'><br>".$row['optie'];"</a><br /><br /> ";
echo '</div>';
$aantal++;
if ($aantal==3) {echo "<tr>"; $aantal=0;}
}
echo "</tr></tr>";
echo "</table>";
?>

You should simply concatenate your value onto the end of your URL.
$row['optie'] = str_replace('\r', '<br>', $row['optie']);

Related

Limiting column for fetched data in php

I am trying to put extracted mysql data into a table using php. The issue is that I am able to put the fetched data into either all rows or all columns. What I would like to do is put the fetched data into a column format and as soon as 4 columns are used, the next data should start on the next row and so on.
Currently I am using the logic below, however I am getting everything in a column.
The goal is to limit the data to a page size so the data can be printed.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
while($row = mysql_fetch_array($result))
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?></div>
If I understand your problem correctly you will want to do something like:
Keep a counter to see which number record you're looking at ($i)
$i = 0; // Your counter
while($row = mysql_fetch_array($result))
{
// ...
On every 4th iteration of the loop output something like </tr><tr>.
if ($i % 4 == 0 && $i > 0) // Will return true if `$i` is a multiple of 4 and greater than 0
{
echo "</tr><tr>"; // Output your HTML to start a new row
}
One final note: As others will be pointing out. You should avoid using the MySQL extension. You should use MySQLi or PDO instead.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
$i = 1;
while($row = mysql_fetch_array($result))
{
if($i == 4)
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
$i=0;
}
$i++;
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?>
</div>

How can I retrieve images from MYSQL- Encrypted characters error

i'm working with uploading and retrieving images from database using php and mysql.I have tried the following codes below but it returns lot of encrypted characters. The data type of the image in mysql is longblob.
while ($getRows = mysql_fetch_array($iQuer)){
$yyy = $getRows['user_id'];
$get = mysql_query("SELECT * FROM users where id ='".$yyy."'");
$divider = mysql_fetch_array($get);
$getName = $divider['user_name'];
$getImage = $divider['profile_pic'];
echo "<tr>";
echo "<td><div id = 'postsdiv'>" . "<img src = ".$getImage."/> <strong>". $getName ."</strong><br>". $getRows['post'] . "</div><br>";
echo "</tr>"; }
I also tried another syntax but it returns nothing:
while ($getRows = mysql_fetch_array($iQuer)){
$yyy = $getRows['user_id'];
$get = mysql_query("SELECT * FROM users where id ='".$yyy."'");
$divider = mysql_fetch_array($get);
$getName = $divider['user_name'];
$getImage = $divider['profile_pic'];
echo "<tr>";
echo "<td> <img src='data:image/png;base64,'" . base64_encode($getImage) . " />'"
echo "<td><div id = 'postsdiv'>" . "<strong>". $getName ."</strong><br>". $getRows['post'] . "</div><br>";
echo "</tr>";
}
Can you try it?
$getImage = $divider['profile_pic'];
header("content-type:image/jpeg"); // Can you add this line
echo $getImage;

Echo first result from MYSQL differently than all the other results

I have a mysql table in which contains video titles, video embed html, video description, and video thumbnails. I want it to output the first entry as;
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}
and after that, I would like it to output the next five entries as
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
however I have no idea how to do this. I am kind of a newb when it comes to mysql. Any help would be appreciated.
I have looked at other similar questions but none of them really fit the bill.
Poor man's solution would be something like:
$result = mysqli_query($con,"SELECT * FROM entries");
$first = true;
while($row = mysqli_fetch_array($result)) {
if ($first) {
echo $row['title'], $row['html'], $row['desc'];
$first = false;
continue 2;
}
echo $row['title'], "<a href='{$row[id]}'><img src='{$row[thu]}'></a>";
}
(As explained in PHP chatroom)
Since mysqli_fetch_array() gets you a row, you can use it before the while() without a problem like this
$result = mysqli_query($con,"SELECT * FROM entries");
$row = mysqli_fetch_array($result);
echo $row['title'];
//etc.
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
Track it with a sentinel value
$i = 0;
while($row = mysqli_fetch_array($result)) {
if($i == 0){
//first iteration
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}else{
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
$i++;//increment value
}

Mysql Field Data not displaying when a link is clicked?

I'm trying to get data from a database if a link is clicked.
I used the example codes suggested from this example -Getting mysql field data when a link is clicked?
But it doesn't work when I click on a link nothing comes up.
main.php
<?php
include('conn.php');
$sql2 = "SELECT Title FROM addpromo";
$result2 = mysql_query($sql2);
echo "<div id=\"links\">\n";
echo "<ul>\n";
while ($row2 = mysql_fetch_assoc($result2)) {
echo "<li> <a href=\"fullproject.php?title=\""
. urlencode($row2['Title']) . "\">"
. htmlentities($row2['Title']) . "</a>\n</li>";
}
echo "</ul>";
echo "</div>";
?>
This is displaying correct.but when I click at a link nothing is showing up in fullproject.php, Just a blank page.
fullproject.php
<?php
// Connect to server.
include('conn.php');
$projectname = isset($_GET['Title']);
$sql1 = "SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1 = mysql_query($sql1);
while ($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title'] . "<br />";
echo "<br /> ";
}
?>
Can someone help me to fix this, or any other way to make this(to get data from a database if a link is clicked) possible?
Change to this
main.php
<?php
include('conn.php');
$sql2="SELECT Title FROM addpromo";
$result2=mysql_query($sql2);
echo '<div id="links">';
echo '<ul>';
while($row2 = mysql_fetch_assoc($result2)){
echo '<li>'.htmlentities($row2['Title']).'</li>';
}
echo '</ul>';
echo '</div>';
?>
fullproject.php
<?php
if(isset($_GET['title'])){
include('conn.php');
$projectname= $_GET['title'];
$sql1="SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title']. "<br />";
echo "<br /> ";
}
}
?>
This is storing a boolean value $projectname= isset($_GET['Title']);, whether or not the title is set. Instead use $projectname = $_GET['Title'];
isset returns a boolean value (true/false) and you want the actual value of the variable:
$projectname= $_GET['title'];
Furthermore, you have to pass only the title as the URL parameter, without enclosing it within quotes. So there is an error in this line:
echo "<li> <a href=\"fullproject.php?title=" . urlencode($row2['Title']) . "\">"
Note the lack of \" after title=

How To Edit/Delete Stored Data From Database In PHP

EDIT
I have a mysql table with fields as follows:
Products - serial, name, description, price, picture.
the viewproducts.php page is as follows:
<?php
$result = mysql_query("SELECT * FROM products ")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Products';
} else {
echo "<table border='0'><table border='1' width=100%><tr><th>Product Name</th><th>Description</th><th>Price</th><th>Image</th><th>Edit</th><th>Delete</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['description']. "</td>";
echo "<td>£" . $info['price']." </td>";
echo "<td>" . "<img src='../getImage.php?id=" . $info['serial'] ."'/></td>";
echo '<td> Edit</td>';
}
}
echo "</tr>";
echo "</table>";
?>
my edit.php page looks like this:
<?php
$product_id = $_GET['serial'];
$result = mysql_query("SELECT * FROM products WHERE serial = '$product_id'")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Products';
} else {
echo "<table border='0'><table border='1' width=100%><tr><th>Product Name</th><th>Description</th><th>Price</th><th>Image</th><th>Edit</th><th>Delete</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['description']. "</td>";
echo "<td>£" . $info['price']." </td>";
echo "<td>" . "<img src='../getImage.php?id=" . $info['serial'] ."'/></td>";
}
}
echo "</tr>";
echo "</table>";
?>
when i click on edit from thr viewproducts.php page, it goes to edit.php page where nothing is showing up. the serial id on the address bar is coming up as follows:
http://www.********.com/****/admin/edit.php?product_id=
I want to be able to edit any product clicked on from the viewproduct.php page and transfered to edit.php page. I dont think my edit.php page is set up corretly.
Please help,
Thanks
You can pass via $_GET the id of the product and then, in the edit/delete page, retrieve that parameter. Obviously you have to sanitize the input properly before using it. For example, the link of the each product should look like this:
echo '<td>Edit</td>';
In the edit page you should have something like:
$id = $_GET['id'];
// For example, if the product id is an integer
// you can sanitize it doing this
$id = (int) $id
You could pass it as an argument to your php file in wich you want to edit/delete the product:
Edit Product
Then in your edit.php you will pick up the id of the product and load it's data from the database.
[edit.php]
$product_id = isset($_GET['product_id']) ? intval($_GET['product_id']) : null;
if(!$product_id) {
exit();
}
// query your database for the product
$row = mysqli_query("SELECT * FROM <table> WHERE product_id = $product_id");
// then you output your html with fields populated from the result from the database

Categories