If Statement To Use For Image / File Link
Hello,
I have a page that displays profile pages. It works well but when the profile does not upload an image, it shows a broken link. So I it works when an image is uploaded fine. But when no image is uploaded I just need no HTML code to be printed.
I need to add an if statement to the display. You can see here the link to see why it needs an if statement.
http://www.touranimalregistry.com/animals/14
Here is the template code that I have used so far but it does not work. I cannot figure out how to test the value of the field in real time to decide if it is empty or not.
<?php if (!empty($medical4)){ } else { echo "<tr><td valign=\"top\"><b>Medical 4:</b> <a href=\"{@medical4, image/full}\" target=\"_blank\">Click To View</a></td></tr>"; } ?>
So $medical is a PDF file. Sometimes in other fields I uplaod photos.
Does anyone have a bit of sample code that I can work form for an if statementto test the field value with? I only need to know how to do the if statement to see if the field is empty or if a file has been uploaded.
Thanks, Matt
14 Answers
Images and other Pick fields return an array, which while it does not contain data may not show up as empty(). Try this:
if ( is_array($medical4) && 0 < count($medical4) ) {
echo ...
}
You could also try:
if ( !empty ($medical4[0]['id']) ) {
echo ...
}
Hello,
Thanks. I tried both but still no worky. I understand that the data is saved as an array but I cannot getthe simple test of empty or not empty to work.
http://www.touranimalregistry.com/animals/14
See the link above it returns negative either way when the field is empty or not.
Here is what I have code right now but I could not get it to work even using the other if statement:
<?php if ( is_array($medical4) && 0 < count($medical4) ) { echo "<tr><td valign=\"top\"><b>Medical 4:</b> <a href=\"{@medical4, image/full}\" target=\"_blank\">Click To View</a></td></tr>"; } ?>
It always returns negative even when I have a PDF file uploaded.
ANy ideas would be great. Looks not so good with all the broken links showing.
Thanks, Matt
How are you populating the data into $medical?
WHen I created this type of field in the pod, I choose "File Upload" as the data type. So I either upload a PDF or JPG into this type of field when creating records.
I get that. Show me all the lines in your code that begin $medical4 = ...
The code below always displays nothing even when there is a PDF uploaded.
<?php if ( is_array($medical4) && 0 < count($medical4) ) { echo "<tr><td valign=\"top\"><b>Medical 4:</b> <a href=\"{@medical4, image/full}\" target=\"_blank\">Click To View</a></td></tr>"; } ?>
I also cannot get the basic text filed test to work either. So for an animal STatus or "Working" or "Deceased" I cannot get iether to work for the test.
<?php if ($status == "Deceased"){ echo "<tr><td valign=\"top\"><b>Death Date:</b> {@death_date}</td></tr>"; echo "<tr><td valign=\"top\"><b>Death Location:</b> {@death_location}</td></tr>"; echo "<tr><td valign=\"top\"><b>Death Method:</b> {@death_method}</td></tr>"; echo "<tr><td valign=\"top\"><b>Resting Location:</b> {@resting_location}</td></tr>"; } ?>
I no idea why either test does not work. I usually can code this type of logic no problem using PERL and ASP.
I can get it to work now when i test just a basic text field to see if it is empty of not. See below example.
<blockquote> <?php $facebookprint = $this->get_field('facebook'); if ($facebookprint != ""){ echo "<tr><td valign=\"top\"><b>Facebook:</b> {@facebook}</td></tr>"; } ?> </blockquote>
Now I just need to figure out if a file has beenuploaded or not when testing one of those fields.
I tried doing what they are doing in this posting to test if the uploaded file is present and I could not get it to work.
http://mondaybynoon.com/20100104/pulling-pods-data/
I cannot get the basic test of if a file has been uploaded or not to work.
So I still end up with broken images and links to PDFs that have not been uploaded. I need to keep the file uploading optional so I need the if statement to work so I can run the test in real time if a file has been uploaded or not.
Any ideas on code to try would be greatly appreciated.
Thanks, Matt
The code you are posting looks good. That tells me that the problem is somewhere else. Can you post all you code?
I have psoted the entire "animal_detail" pod template code below. You have access already via the uhoh@... email address if you want to look. As you can see below I can get the basic IF statements working, just not the if statements for the array values like file upload. I reset the $medical4 if statment back to what is was before with default display since I could not get it to work.
<table width="100%" border="0" cellpadding="30" cellspacing="0"> <tr> <td valign="top" >
<table width="100%" border="0" cellpadding="6" cellspacing="0"> <tr><td valign="top"><h3 class="h3">Tour Animal Profile</h3></td></tr> <tr> <td valign="top"> <?php foreach ($this->get_field('owner_photo') as $photo) { echo "<img width=\"600\" src=\"{$photo['guid']}\" />"; } ?> </td></tr> <tr><td valign="top"><b>Registry ID:</b> {@id}</td></tr> <tr><td valign="top"><b>Animal ID:</b> {@animal_id}</td></tr> <tr><td valign="top"><b>Name:</b> {@name}</td></tr> <tr><td valign="top"><b>Animal Type:</b> {@animal_type}</td></tr> <tr><td valign="top"><b>Date Added:</b> {@date_added}</td></tr> <tr><td valign="top"><b>Tour Company:</b> {@company_ownership, pick_one}</td></tr> <tr><td valign="top"><b>Animal Owner:</b> {@tour_ownership, pick_one}</td></tr> <tr><td valign="top"><b>Work Type:</b> {@work_type}</td></tr> <tr><td valign="top"><b>Status:</b> {@status} </td></tr> <tr><td valign="top"><b>Gender:</b> {@gender}</td></tr> <tr><td valign="top"><b>Breed:</b> {@breed}</td></tr> <tr><td valign="top"><b>Registry Entry Weight:</b> {@registry_entry_weight}</td></tr> <tr><td valign="top"><b>Birth Date:</b> {@birth_date}</td></tr> <tr><td valign="top"><b>Birth Location:</b> {@birth_location}</td></tr> <?php $statusprint = $this->get_field('status'); if ($statusprint == "Deceased"){ echo "<tr><td valign=\"top\"><b>Death Date:</b> {@death_date}</td></tr>"; echo "<tr><td valign=\"top\"><b>Death Location:</b> {@death_location}</td></tr>"; echo "<tr><td valign=\"top\"><b>Death Method:</b> {@death_method}</td></tr>"; echo "<tr><td valign=\"top\"><b>Resting Location:</b> {@resting_location}</td></tr>"; } ?> <tr><td valign="top"><b>Housing Location:</b> {@housing_location}</td></tr> <tr><td valign="top"><b>Washing Schedule:</b> {@washing_schedule}</td></tr> <tr><td valign="top"><b>Inspection Schedule:</b> {@inspection_schedule}</td></tr> <tr><td valign="top"><b>Deworming Schedule:</b> {@deworming_schedule}</td></tr> <tr><td valign="top"><b>Veterinarian:</b> {@veterinarian}</td></tr> <tr><td valign="top"><b>Rabies Shot:</b> {@rabies_shot}</td></tr> <tr><td valign="top"><b>Five Way Shot:</b> {@five_way_shot}</td></tr> <tr><td valign="top"><b>Spaying / Neutering:</b> {@spaying_neutering}</td></tr> <tr><td valign="top"><b>Number Of Mates:</b> {@number_of_mates}</td></tr> <tr><td valign="top"><b>Number Of Kin:</b> {@number_of_kin}</td></tr> <tr><td valign="top"><b>Days Off Per Week:</b> {@days_off_per_week}</td></tr> <tr><td valign="top"><b>Off Season Exercise:</b> {@off_season_exercise}</td></tr>
<?php $facebookprint = $this->get_field('facebook'); if ($facebookprint != ""){ echo "<tr><td valign=\"top\"><b>Facebook:</b> <a target=\"_blank\" rel=\"nofollow\" href=\"{@facebook}\">{@facebook}</a></td></tr>"; } ?>
<?php $youtubeprint = $this->get_field('youtube'); if ($youtubeprint != ""){ echo "<tr><td valign=\"top\"><b>Youtube:</b> <a target=\"_blank\" rel=\"nofollow\" href=\"{@youtube}\">{@youtube}</a></td></tr>"; } ?>
<tr><td valign="top"><b>History:</b> {@history}</td></tr> <tr><td valign="top"><b>Injury History:</b> {@injury_history}</td></tr> <tr><td valign="top"><b>Owner Notes:</b> {@owner_notes}</td></tr> <tr><td valign="top"><b>Registry Notes:</b> {@registry_notes}</td></tr> <tr><td valign="top"><b>Medical 1:</b> <a href="{@medical1, image/full}" target="_blank">Click To View</a></td></tr> <tr><td valign="top"><b>Medical 2:</b> <a href="{@medical2, image/full}" target="_blank">Click To View</a></td></tr> <tr><td valign="top"><b>Medical 3:</b> <a href="{@medical3, image/full}" target="_blank">Click To View</a></td></tr> <tr><td valign="top"><b>Medical 4:</b> <a href="{@medical4, image/full}" target="_blank">Click To View</a></td></tr>
<tr> <td valign="top" align="center"><b>Owner Photo 2:</b><br> <div align="center"><img src={@owner_photo2, image/full} width="800" border="0"></div> </td></tr> <tr> <td valign="top" align="center"><b>Owner Photo 3:</b><br> <div align="center"><img src={@owner_photo3, image/full} width="800" border="0"></div> </td></tr> <tr> <td valign="top" align="center"><b>Owner Photo 4:</b><br> <div align="center"><img src={@owner_photo4, image/full} width="800" border="0"></div> </td></tr> <tr> <td valign="top" align="center"><b>Owner Photo 5:</b><br> <div align="center"><img src={@owner_photo5, image/full} width="800" border="0"></div> </td></tr> <tr> <td valign="top" align="center"><b>Registry Photo 1:</b><br> <div align="center"><img src={@registry_photo1, image/full} width="800" border="0"></div> </td></tr> <tr> <td valign="top" align="center"><b>Housing Photo:</b><br> <div align="center"><img src={@housing_photo, image/full} width="800" border="0"></div> </td></tr> <tr> <td valign="top" align="center"><b>Housing Photo 2:</b><br> <div align="center"><img src={@housing_photo2, image/full} width="800" border="0"></div> </td></tr> <tr> <td> <br> <p><b><a href="/animals/">Return To Main Animals Page</a></b></p> </td> </tr> </table>
</td> </tr> </table>
I tried againto figure this out and could not.
I tried using this code and it still will not work. I can printthe variable data (file location) no problem atthe top ofthe page. But when I go to test it for a matching string it never works.
<?php $medical4print = "{@medical4, image/full}"; echo "Medical 4 test: $medical4print"; if (preg_match("/uploads/", "$medical4print")) { echo "Medical 4: $medical4print"; } ?>
So I am still stuck on this. No idea how to figure it out. Try to test the fields so I do not show the dead image code when no file has been uploaded.
I can printthe value of the $medical 4 field no problem on the page, but as soon as I use it in an if statementit does not work for me.
You can't use that syntax to assign a variable. Using {@medical4} in your template will echo the variable and not assign it, so $medical4print will always be NULL.
The best way to do this is to have your if logic in your "image/full" helper. If you can't, you could use output buffering (look at ob_start() ) to capture the output into a variable.
Thanks for the udpate. I looked at the helper code and it appears I can set the default there. But when I try it generates an error.
<?php // you can set a default image url to show // if no image was uploaded, below $default = '';
I try setting it to this but it did not work.
$default = "/images/blank.jpg'';
Any idea what I should make that line to reference the blank image when no file has been uploaded?
Thanks, Matt
Copy and paste that line of code into notepad, and you'll see what the problem is. You open it with a double quote and close it with two single quotes. This is your code above with an extra space added after each quote character for clarity.
$default = " /images/blank.jpg' ' ;


