getting two kinds of data and display it out [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table called "profile" in mysql database
Table structure:
user | hasimg | imgurl
As you can see "hasimg" and "imgurl", normally if a user does not have an image, hasimg will be set to 0 and imgurl is set to none.
On the other hand, if a user has an image assigned then hasimg is set to 1 and imgurl will contain the link.
How can I display ones that have images (hasimg set to 1) with <img src="imgurl html tags?
How can I display ones that do not have images (hasimg set to 0) , without <img src?
I want to make a list which display users with images and user that does not have image.
Here's the code im using to echo out the data row by row
<?php
$sql = "SELECT * FROM profile ORDER BY user DSC";
$query = mysql_query($sql)or die(mysql_error());
$rows = array();
while($row = mysql_fetch_array( $query )){
$rows[] = $row;
echo "User:$row[user]<br>Image:$row[hasimg]\n";
}
?>
P/S mysql connection established.

If you want to keep your layout fancy way you can use default image.
if($hasimg == 0)
$imgurl = "default.jpg";
echo '<img src="' . $imgurl . '">';
EDIT:
Your PHP code could be like this.
$sql = "SELECT * FROM profile ORDER BY user DSC";
$result = mysql_query($sql)or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Image</th>
</tr>";
while($row = mysql_fetch_array($result))
{
$imgurl = $row['imgurl'];
if($row['hasimg'] == 0)
$imgurl = "default.jpg";
echo "<tr>";
echo "<td>" . $row['user'] . "</td>";
echo "<td><img src=\"" .$imgurl . "'\" /></td>";
echo "</tr>";
}
echo "</table>";

Two solutions:
Set a pathway to default img jpg file as default field content to field imgurl in your database.
Filter query results by adding this code just after $rows[] = $row;:
If ($row['hasimg'] == 0) $row['imgurl'] = 'default.jpg';

Related

image not picking up the path [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
this my code but unable to display image from db in php.i think this is not pick up the path.any one can help in this regard.
<?php
include('connect.php');
$result = $db->prepare("SELECT image FROM info WHERE empid= '". $empid ."'");
$result->bindParam('. $empid .', $empid);
$result->execute();
for($i=0; $rows = $result->fetch(); $i++){
echo '<img src="images/".$row["image"]." ">';
echo '<img src="images/".$row["image"]. > ' ;
}
?>
You either concatenate the id onto the text string containing the query or use a parameter place holder and then bind a value to it. Not both, as you were doing.
The most secure way is to use parameters.
<?php
include('connect.php');
// I assume you have set $empid somewhere in the missing code here
$result = $db->prepare("SELECT image FROM info WHERE empid= :empid");
$result->bindParam(':empid', $empid, , PDO::PARAM_INT);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
// also changed these 2 rows to correct the concatenation
echo '<img src="images/"' . $row["image"] . '">';
echo '<img src="images/"' . $row["image"] . '">';
}
?>

PHP image display from mysql from different tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am creating a used car website for a class. I have a mysql database that has two tables. One for the car details and one table for car photos. I am trying to display 1 single photo of the car next to some details (like you see on cars.com) but I am struggling. The car id is one one table and the "ext_1" in on another table.
<?php
$page_title = "List cars";
require_once ('includes/header.php');
require_once('includes/database.php');
//select statement
$sql = "SELECT * FROM inventory_tbl, inventory_photos_tbl";
//execute the query
$query = $conn->query($sql);
//Handle selection errors
if (!$query) {
$errno = $conn->errno;
$errmsg = $conn->error;
echo "Selection failed with: ($errno) $errmsg<br/>\n";
$conn->close();
//require_once ('includes/footer.php');
exit;
}
//display results in a table
?>
<h2>Inventory</h2>
<table border="1px solid" border="collapse" align ="center" height='300' width='750' >
<tr><center>
<th></th>
<th>Year</th>
<th>Make</th>
<th>Model</th>
<th>Mileage</th>
<th>Price</td>
<th>Details</th>
</center>
</tr>
<?php
//insert a row into the table for each row of data
while (($row = $query->fetch_assoc()) !==NULL){
echo "<tr>";
echo "<td><img src =" .$row['id']. $row['ext_1']. " width = 100 height = 100></td>";
echo "<td>", $row['year'], "</td>";
echo "<td>", $row['make'], "</td>";
echo "<td>", $row['model'], "</td>";
echo "<td>", number_format($row['mileage']), "</td>";
echo "<td>","$", number_format($row['price']), "</td>";
echo "<td><a href='cardetails.php?id=", $row['id'],"'>View Details</td>";
echo "</tr>";
}
?>
<?php
// clean up resultsets when we're done with them!
$query->close();
// close the connection.
$conn->close();
//include the footer
require_once ('includes/footer.php');
I'm moving my answer out of the comments, so future readers are able to benefit from the solution.
First, the query needs to qualify the join condition, so change the query to:
$sql = "SELECT * FROM inventory_tbl, inventory_photos_tbl WHERE inventory_tbl.id = inventory_photos_tbl.ext_1"
Second, the img tag should only refer to the photo source/column, so it should be something like:
echo "<td><img src =" .$row['myPhotoColumn']. " width = 100 height = 100></td>";
What you're wanting to do is a left join. (Join data from the right table into the left table if the data in the right table exists).
Your SQL statement would look like this.
$sql = "SELECT * FROM inventory_tbl
LEFT JOIN inventory_photos_tbl as images
ON images.ext_1 = car_id";
Images serves as an alias to your photos table.
You match up images.ext_1 column to the main table's car_id column which is what the ON statement signifies. Think of it as a where clause for the JOIN
Now you can reference any of the data in the inventory_photos_table you want in your PHP using your $row variable such as $row['image_column_name'].
You'll probably want to wrap the image data in an if statement to show a placeholder image in case there isn't an image associated with the vehicle yet.
As i don't know your table structure. I assumed the relational column name as 'id' in inventory_photos_tbl table.
<?php
$sql = "SELECT * FROM inventory_tbl";
$query = $conn->query($sql);
while (($row = $query->fetch_assoc()) !==NULL){
$carID=$row['id'];
// In this query, i'm assuming the relational column as 'id'. (If you are having different column name, please change it here itself)
$imgQuery = $conn->query("SELECT * FROM inventory_photos_tbl WHERE id=$carID LIMIT 0,1");
while(($rowImg = $imgQuery->fetch_assoc()))
{
$carIMG=$rowImg['ext_1'];
}
echo "<tr>";
echo "<td><img src ="$carID.$carIMG" width = 100 height = 100></td>";
echo "<td>", $row['year'], "</td>";
echo "<td>", $row['make'], "</td>";
echo "<td>", $row['model'], "</td>";
echo "<td>", number_format($row['mileage']), "</td>";
echo "<td>","$", number_format($row['price']), "</td>";
echo "<td><a href='cardetails.php?id=", $row['id'],"'>View Details</td>";
echo "</tr>";
}?>

how print false if column value is 0 or less than 0 or null,print true if value is greater than 0 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am trying to make a web page that fetch value from database .i fetch 3 column from table councel name councel name ,smscount emailcount,i want if smscount and emailcount value is null or 0 or less than 0 then print false and if value is greater than 0 then print true.
how can i do this task.
code for getting value from table column is
<?php
// Connects to your Database
mysql_connect("localhost", "root","root") or die(mysql_error());
echo "connec to local host";
mysql_select_db("Court") or die(mysql_error());
echo "connec to Database";
$data = mysql_query("SELECT * FROM councel")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print " <td>".$info['CouncelName'] . "</td> ";
Print " <td>".$info['SMSCount'] . "</td> ";
Print " <td>".$info['EmailCount'] . " </td></tr>";
}
Print "</table>";
?>
![enter image description here][1]
assuming that your values are integers you can just do this way:
if($info['EmailCount'] && $info['SMSCount'] && $info['EmailCount'] >= 0 && $info['SMSCount'] >= 0){
echo "true";
}else{
echo "false";
}

Only the first entry from database is displayed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have 2 php files that retrieve a BLOB image from mysql DB. My DB has stored a couple of different images, as i try to display them on the browser, only the first image is displayed multiple times, for example: The DB table has stored 5 images, the browser is going to display the first image 5 times.
here is a snippet from my main php file:
$strSQL = "SELECT * FROM images";
$rs = mysql_query($strSQL) or die (mysql_error());
echo "<table>";
while($row = mysql_fetch_array($rs)) {
echo "<tr><td>";
echo " <img src=load_pic.php?id=".$row["id"]." id='img' width='100' height='100'></a>";
echo "</td></tr>";
}
echo "</table>"
and the php file that gets the images "load_pic.php"
$q="select * from images";
$rec=mysql_fetch_array(mysql_query($q));
$data=$rec['image'];
header('Content-Length: '.strlen($data));
header("Content-type: image/".$rec['type']);
echo $data;
Your load_pic.php script is not using the id parameter. It should be:
$q = "select * from images where id = " . mysql_real_escape_string($_GET['id']);

PHP/MySQL OnClick Update MySQL

Need to update a hitcount field in a MySQL table when a user clicks on a banner ad. Have the random ad display script working but can't figure out how to update the table when they click..assuming will have to pass the ID to Ajax but no idea how to approach it? Code is below:
include 'connection.php';
$query = "select * from ads where adtype = 'small' and status = 'yes' ORDER BY RAND() LIMIT 3";
$result = mysql_query($query) or die(mysql_error());
$num_results = mysql_num_rows($result);
if ($num_results !="0")
{
for($i=0;$i<$num_results;$i++)
{
$row = mysql_fetch_array($result);
$client = htmlspecialchars(stripslashes($row['client']));
$link = htmlspecialchars(stripslashes($row['link']));
$filename = htmlspecialchars(stripslashes($row['filename']));
$id = $row['id'];
echo "<tr>";
echo "<td>";
echo '<a href="';
echo $link;
echo '"><img src="thimg/';
echo $filename;
echo '" alt="';
echo $client;
echo '"></a>';
echo "</td>";
echo "</tr>";
}
}
Make the link point to a page which takes the ID of the ad as a parameter, something like click.php?id=the_id. Then that page can update the database, look up the link, and then you can use a header redirect to forward them on to the link. Make sure you don't output anything on that forwarding page though, or the redirect won't work.
This should get you what you need, without the need for javascript or ajax.

Categories