Displaying default image - php

I have a default image set in the db in the column default_image. If the default_image = 1 I want it to display the default image and if default_image = 0 I want text to say no image available.
How do I make sure only the default image is displayed. Here is what I am doing now and it just displays any image that has the same link_id.
the column media_link is the address to the image.
<?php if (!empty($row['default_image'])){
echo "<a href='http://localhost/images".$row['media_link']."'><img src='http://localhost/images".$row['media_link']."'/></a>";
}
else{
echo "<div align=\"center\">No image available</div>";
}?>
What do I do to make default_image "1" in the database display and nothing else? Thanks.
EDIT:
Here is another piece of code to show all the images for that link_id
if ($row['media_link']){
echo "<a href='http://localhost/t_images".$row['media_link']."'><img src='http://localhost/images".$row['media_link']."' /></a>";
}
else{
echo "<div align=\"center\">No images available</div>";
}
}
there is 6 images for this current id
When i echo the vardump on this page the correct image has this
string(1) "1"
while the other 5 images "non default"
show this string(1) "0"
I am not sure how to show only the correct default image where it goes on the page in my first code.
Does this info help at all?
EDIT AGAIN/FIXED:
I fixed it by adding ORDER BY default_image DESC to the end of my query. Don't know if that is the best thing to do but if anyone has any other suggestions i would be glad to hear them. Thanks everyone for all your help. You all gave great answers but in the end I just added the order by statement to my query and it works that way so I am happy.
Thanks!

In your situation, $row['default_image'] is a string. Of course, PHP is not a strongly typed language, but it still has to figure out what you mean. In your example, you are expecting this:
empty("0") === true
which does not happen. The empty string "" is empty, the number 0 is empty, but the string "0" is not. Instead you could use either of:
$row['default_image'] !== "0"
// Uses strict comparison, which might not be what you expect
0+$row['default_image'] != 0
// Uses casting, which might fail if the string doesn't contain a number
$row['default_image'] != "0" || !empty(0+$row['default_image'])
// Uses short-circuiting, and should cover all (most) cases
There are other variants. Myself, I am not sure which I would choose between them: it depends on what you mean, exactly.

Check Default_image against 1 instead of empty.
try: ..
<?php
if( isset($row['default_image']) && $row['default_image'] == 1){
echo "<a href='http://localhost/images".$row['media_link']."'><img src='http://localhost/images".$row['media_link']."'/></a>";
}else{
echo "<div align=\"center\">No image available</div>";
}
?>

try this ...
<?php if (!empty($row['default_image']) && $row['default_image'] == 1){
echo "<a href='http://localhost/images".$row['media_link']."'>
<img src='http://localhost/images".$row['media_link']."'/></a>";
}
else{
echo "<div align=\"center\">No image available</div>";
}?>

Related

Not able to Display without storing it in DB

I am not sure whether I am displaying the image in the right way or not. I am not able to display the image in the given field and the images are not stored in DB ...it's a gender based avatar choosing script (I hope so).
if (isset($_GET['gender']) == 'm') {
echo ' <img id="avat" src="imgs/avatar-boy-1.png" alt="">';
} elseif (isset($_GET['gender']) == 'f') {
echo ' <img id="avat" src="imgs/avatar-girl-1.png" alt="">';
}
Funk Forty Niner and Dharman pointed this out in the comments, but isset($_GET['gender'])=='m' doesn't make sense.
isset($_GET['gender']) returns a boolean, true or false, telling you whether $_GET['gender'] is set. You can't meaningfully compare that boolean to 'm'.
You probably want something like isset($_GET['gender']) && $_GET['gender'] == 'm'.
Or just move the isset($_GET['gender']) into its own if statement, since you are checking it both times.

php string comparison not working for image metadata

I am extracting image metadata using php. The logic of my below code is that if the user uploads the default file with metadata(UserComment)=ASCIIsd11, he/she will get an error.
<?php
$exif_s = exif_read_data('e42889ed00.jpg');
$phtchk = $exif_s["UserComment"];
print $phtchk;
print strcmp($phtchk, "ASCIIsd11");
if(strcmp($phtchk, "ASCIIsd11") == 0){ echo "You have not uploaded your own photo"; exit;}
else
{
echo"You have uploaded it.";
}
?>
print $phtchk; returns ASCIIsd11
print strcmp($phtchk, "ASCIIsd11"); returns -1
and the last echo statement "You have uploaded it" is printed. Actually I am expecting strcmp() to return 0. Kindly help.
Do var_dump(phtchk); instead of print $phtchk;
Perhaps you don't see some extra-chars (eg: \n).
If it concerns the collation, you should see:
UTF-8 characters not displaying properly from JPEG IPTC data in PHP

Using if else statement based off of $_POST value to echo message

Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value

How To Set A PHP GET Variable If It's Not Set

This is what I have.
<?php
if (isset($_GET["var"])) {
echo "1";
} else {
echo "1";
}?>
I want it to check if there's been a variable set for "var" and if there isn't (which will happen the first time anyone goes to site), I want it to be set to "1". Then I want to create buttons "back" and "forward" that will will increase "var" by "1" until it reaches my limit of "10" then returns to one.
I can figure out the math and buttons part, I just need help initially setting "var" since it isn't already set when you go to the page.
Aside from architecture, security and any other issues:
$_GET is just a simple array accessible from anywhere in script.
$_GET['var'] = 'foo';
Docs: http://www.php.net/manual/en/language.variables.superglobals.php
Try this tag
<a her="www.example.com?var=<?php echo $get_var?>">your site name</a>
the get variables come in to the script like so
scriptname.php?getvar=val
To "set" the variable you would need to test for it, Increment it and redirect too it
$var = 1;
if (isset($_GET['var'])){
$var = (int) ($_GET['var'] +1);
if($var > 10) {
$var =1;
}
}
header("Location: " . $_SERVER['PHP_SELF'] ."?var=" . $var);
as mentioned in the comments, SESSION is a much better place to store this information

auto hyper-link

I am trying to auto generate pages.
What I am trying to do is make a upload form for an image upload that's displayed in a gallery.
I have this but I then want each image to have a page hyper-link auto made for each image where the image can be seen bigger with buying information that's also been uploaded to a MySQL table not 100% with codeigniter I am still luring please look at the site I am trying to build at http://www.fresherdesign.co.uk/PIFF/index.php/main/gallery
This is a direct link to the gallery that I would link to make the page's from, currently they just open a direct link to the image on its own
Any help would be awesome thanks to everyone in advance
Alan Morton
If you want the actual images to be uploaded (and not stored in the database - note that db storage is slightly more difficult to accomplish than your standard upload), you can create a field in the database for the image's location; that is how you are going to correspond your image data with your page content.
Now, if you want a hyperlink to automatically be made, I suggest querying the database of all "Active" entries (again, could be another field unless you simply delete old entries). Each entry should have a unique ID associated with it. This way, when you give the list, simply include a tag similar to
while($row = mysql_fetch_array($query_result)){
// Change this to whatever formatting you need
print '<!-- Whatever content for link -->';
}
Now that's just your loop to get the results. The actual page now should query the database based on the ID given. The query should grab the page content from the database. Something like this would work
<?php
if(!isset($_GET['id'])){
die("Please use a valid link!");
}
$q = mysql_query("SELECT * FROM YOUR_DB_TABLE WHERE ID='".mysql_real_escape_string($_GET['id'])."' LIMIT 1;");
if(!$q || mysql_num_rows($q) < 1){
die("A MySQL Error Occurred: ".mysql_error());
}
while($row = mysql_fetch_array($q)){
// Again, adjust this accordingly
print "Data column 1: ".$row['DataRow1']."<br />".
"Data Column 2: ".$row['DataRow2']."<br />".
"<img src='".$row['ImageLocation']."' />";
}
?>
Now, I haven't tested this exact code, however all of it should work as is. But you will need to adjust it appropriately to fit your requests; but this is one way to accomplish what you're looking for.
Precondition
You'll need to have the directory name in which things are stored, relative to where your php page is located. Obviously, you have this already to create the page. I will assume it is stored in the $dir variable.
Code
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
echo "<a href='http://www.mydomain.com/".$dir."/".$file."'>";
echo "<img src='http://www.mydomain.com/".$dir."/".$file."' />";
echo "</a>";
}
}
Output
This will provide you a list of images that link to the image file itself.
Possible Modifications
1) You may want to only show some of the files in the directory, or a certain amount: Use the if block added here to do that:
if ($handle = opendir($dir)) {
//set $strThatSpecifiesImages to whatever you want;
//for the example, I'm saying that we only want to show files with "gal_image" in the filename
$strThatSpecifiesImages = "gal_image";
$maxFilesToShow = 10; //set this to whatever you want; example max is 10
$count = 0;
while (($count < $maxFilesToShow) && (false !== ($file = readdir($handle))) {
if (strpos($file, $strThatSpecifiesImages) > -1) {
echo "<a href='http://www.mydomain.com/".$dir."/".$file."'>";
echo "<img src='http://www.mydomain.com/".$dir."/".$file."' />";
echo "</a>";
$count++;
}
}
}
2) You might also want to change how things are displayed, so that you don't just have the full image shown here every time. Do this by modifying things in the echo blocks that are outputting HTML. You can have the browser resize the images, or just change it completely so that the links use text instead, or something else.

Categories