adman4054 Posted December 15, 2010 Share Posted December 15, 2010 Hello-- Very new to PHP and looking for some help. I have found several examples, but none that I could get to work. I'm trying to write some code that checks to see if there data contained in a field. If there is I want to show an image from a folder. If there isn’t data, nothing would display. This is a site with three listing levels. The top level, called premium ("p" as the listing type) and can contain a video, so I would like to "If [listing type] in the "listings" table = "P" and there is a video in the video table for this listing then display /images/sample.jpg (image is not dynamically called). The image will link to the listing. I.e., href=”company/php?listID=6 The db is "test", the video table is "video" and there are three fields; "video path", "video image" and "listing ID". The other table involved is “listings” where the listing type is stored. Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 Hey adman4054. Please post your code so we can see if there is something wrong and where so we can help you! Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 Hello Solon-- Thanks for looking at my post. I'm posting two codes that I found while searching, but me being so new to PHP I'm not sure how to make them work This code looks like its checking to make sure the field is not empty, but is it just checking for image 2? Also, "row_rs radiators" am I getting that from a query? <?php if (!empty($row_rs_radiators['image2'])) { echo $row_rs_radiators['image2']; } ?> This example also looks like it would work, but I'm not sure how to customize it for my use <?php if (!empty($mySobi->customFieldsData['field_myvideofield'])) echo <img src=\"/images/stories/has_video.jpg\">; ?> Thanks again. Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 ok... Could you provide all the code? mysql queries and all? With the information you provided i can only guess what each line does... Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 Thank you for the assistance, not sure I'm giving you the right stuff, but here goes: $query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()"); while($detail = mysql_fetch_array($query)) { //echo "<br>".$detail['listingid']; $listingID = $detail['listingid']; $query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()"); while($get = mysql_fetch_array($query2)) { $imageName = $get['logoImage']; if($imageName == ''){ $imageName = 'no_image.jpeg'; } $phone = $get['listPhone']; $toll= $get['listToll']; $query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' "); $numRowsVideo_sr = mysql_num_rows($query2); //$numRowsVideo_sr = 0; Quote Link to comment Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 Thank you for the assistance, not sure I'm giving you the right stuff, but here goes: $query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()"); while($detail = mysql_fetch_array($query)) { //echo "<br>".$detail['listingid']; $listingID = $detail['listingid']; $query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()"); while($get = mysql_fetch_array($query2)) { $imageName = $get['logoImage']; if($imageName == ''){ $imageName = 'no_image.jpeg'; } $phone = $get['listPhone']; $toll= $get['listToll']; $query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' "); $numRowsVideo_sr = mysql_num_rows($query2); //$numRowsVideo_sr = 0; to actually get an error from the mysql_query $result=mysql_query($query) or die(mysql_error()); Do you at all connect to mysql, and then choose database? if(empty($get['logoImage'])){ $imageName = 'no_image.jpeg'; }else{ $imageName = $get['logoImage']; } Maybe you should look into LEFT JOIN? =o Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 Try it like this and let us know if you get any errors. $query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()") or die("Query 1:".mysql_error()); while($detail = mysql_fetch_array($query)) { //echo "<br>".$detail['listingid']; $listingID = $detail['listingid']; $query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()") or die("Query 2: ".mysql_error()); while($get = mysql_fetch_array($query2)) { $imageName = $get['logoImage']; if($imageName == ''){ $imageName = 'no_image.jpeg'; } $phone = $get['listPhone']; $toll= $get['listToll']; $query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ") or die("Query 3:".mysql_error()); $numRowsVideo_sr = mysql_num_rows($query2); //$numRowsVideo_sr = 0; Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 Thanks, No errors. Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 and it does not give you the correct output? Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 I'm so sorry, I'm confusing you. I attached a set of queries that where not really related to what I'm trying to accomplish. I thought that by showing you the queries it would give you an idea of how the code is structured and the table information. The "image and "no image" isnt part of the task I need to accomplish. I'm trying to look into the video table, if there is a video for a particular listing it will pull an image from mu images folder.-- if video exists (by whatever query will work) then display a video icon from /images/video.gif. Sorry, appreciate the help. Quote Link to comment Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 I believe you could write it all as one mysql query... if you just gave us the actual code... Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 or you can place the last query into the second while loop and create an if statement saying that if there is a video for this listing echo the image else echo nothing like so: <?php $query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()") or die("Query 1:".mysql_error()); while($detail = mysql_fetch_array($query)) { //echo "<br>".$detail['listingid']; $listingID = $detail['listingid']; $query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()") or die("Query 2: ".mysql_error()); while($get = mysql_fetch_array($query2)) { $imageName = $get['logoImage']; if($imageName == '') { $imageName = 'no_image.jpeg'; } $query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ") or die("Query 3:".mysql_error()); $numRowsVideo_sr = mysql_num_rows($query2); if($numRowsVideo_sr >= 1) { echo "<img src=''/>"; } else { echo "NO IMAGE"; } } $phone = $get['listPhone']; $toll= $get['listToll']; //$numRowsVideo_sr = 0; ?> i did not test this but it should work. Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 Appreciate you spending the time. I'm getting "NO IMAGE" even when a listing has a video, thoughts? BTW, no errors. Thank you. Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 in $query2 change $listID to $listingID Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 It wasn't very fond of that change, page wouldn't load, no error message Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 Try it now: <?php $query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()") or die("Query 1:".mysql_error()); while($detail = mysql_fetch_array($query)) { //echo "<br>".$detail['listingid']; $listingID = $detail['listingid']; $query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()") or die("Query 2: ".mysql_error()); while($get = mysql_fetch_array($query2)) { $imageName = $get['logoImage']; if($imageName == '') { $imageName = 'no_image.jpeg'; } $listID = $get['listingID']; $query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ") or die("Query 3:".mysql_error()); $numRowsVideo_sr = mysql_num_rows($query2); if($numRowsVideo_sr >= 1) { echo "<img src=''/>"; } else { echo "NO IMAGE"; } } $phone = $get['listPhone']; $toll= $get['listToll']; //$numRowsVideo_sr = 0; ?> Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 Parse error: syntax error, unexpected '}' in .... Sorry Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 can you post the complete error? Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 Parse error: syntax error, unexpected '}' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\demo6\showFeatured.php on line 405 Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 what is on line 405 in your code? Quote Link to comment Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 what is on line 405 in your code? would be nice with some few lines before it and a couple after too. Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 This is the start of the new code and begins on line 248 and ends at line 406, the 405 line is "}" thanks again. <?php include("inc/conn.php"); $query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()") or die("Query 1:".mysql_error()); while($detail = mysql_fetch_array($query)) { //echo "<br>".$detail['listingid']; $listingID = $detail['listingid']; $query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()") or die("Query 2: ".mysql_error()); while($get = mysql_fetch_array($query2)) { $imageName = $get['logoImage']; if($imageName == '') { $imageName = 'no_image.jpeg'; } $listID = $get['listingID']; $query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ") or die("Query 3:".mysql_error()); $numRowsVideo_sr = mysql_num_rows($query2); if($numRowsVideo_sr >= 1) { echo "<img src=''/>"; } else { echo "NO IMAGE"; } } $phone = $get['listPhone']; $toll= $get['listToll']; //$numRowsVideo_sr = 0; ?> <!-- re-skining --> <form action="showFeatured.php?listID=<?php echo $listingID;?>&catID=<?php echo $catID;?>" method="post"> <div style="border:1px solid gray;background-color: offwhite; width: 620px; height:120px;padding:9px;margin-bottom:5px;"> <div style='float:left;width:140;height:120;padding-right:10px;border-right:1px solid gray;'> <a href="companyDetails.php?listID=<?php echo $listingID;?>"><img src='logoImages/<?php echo $imageName; ?>' border='0' height="100px" width="210px"></a> </div> <div style='float:left;width:325px;height:90;padding:5px;'> <div class="data" style="color:#2c5c97;font-family:Geneva, Arial, Helvetica, sans-serif;font-weight:bold;font-size:16px"> <?php echo ucwords($get['listTitle']);?> </div> <div style="color:black;font-size:12px;"> <?php $desc1 = ucfirst(stripslashes($get['listDesc1'])) . " "; $desc1 .= ucfirst(stripslashes($get['listDesc2'])); if(strlen($desc1) > 100){ $strpos = strpos($desc1," ",100); $desc1 = substr($desc1,0,$strpos); echo $desc1."..."; } else { echo $desc1."..."; } ?> </div> <span><a href="companyDetails.php?listID=<?php echo $listingID;?>" style="color:#1F5291;font-Geneva, Arial, Helvetica, sans-serif;font-size:12px;text-decoration:none;">Read More >></a></span> </div> <div style="float:left;width:200;height:27;border:0px solid gray;padding:8px 8px;"> <?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?> <span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/social.gif" width="20" height="26" border="0" title="Social Media"></a> </span> <?php } ?> <?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?> <span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/whitepaper.gif" width="16" height="26" border="0" title="Case Study"></a> </span> <?php } ?> <?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?> <span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/podcast.gif" width="17" height="26" border="0" title="Podcast"></a> </span> <?php } ?> <?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?> <span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/contact.gif" width="19" height="26" border="0" title="Contact"></a> </span> <?php } ?> <?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?> <span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/video.gif" width="20" height="26" border="0" title="Video"></a> </span> <?php } ?> </div> </div> </form> <!-- re-skining eof--> <!-- <form action="showFeatured.php?listID=<?php echo $listingID;?>&catID=<?php echo $catID;?>" method="post"> <tr> <td width='138' height='50' rowspan='3' align='left' valign='middle' bgcolor='#ffffff' style='border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black;'> <div align='left'><img src='logoImages/<?php echo $imageName; ?>' border='0' style='max-width:120;max-height:60;'></div> <td width="255" rowspan="3" valign="middle" style="border-top: 1px solid black; border-bottom: 1px solid black;"> <div class="bodyTxtLGbold"><?php echo $get['listTitle'];?></div> <div class="bodyTxtColor"><?php echo stripslashes($get['listDesc1']) . " ". stripslashes($get['listDesc2']);?></div> <span id="userButton"> <input name="sendToUrl" type="submit" value="<?php echo $get['url'];?>" class="buttonAsListingLink" onMouseOver="this.className='buttonAsListingLink_hover';" onMouseOut="this.className='buttonAsListingLink';"> </span> <input type="hidden" name='url' value="<?php echo $get['url'];?>"> </td> <td width="31" height="28" valign="middle" style="border-top: 1px solid black"><div align="center"><img src="images/arrow.jpg" width="23" height="20"></div></td> <td width="200" valign="middle" style="border-top: 1px solid black; border-right: 1px solid black;"> <span class="bodyTxt" ><a href="companyDetails.php?listID=<?php echo $listingID;?>" class="contactLink">Company Details</a></span> </td> </tr> </form> <tr> <td height="25" valign="top" ><div align="center"><img src="images/phone.jpg" width="23" height="20"></div></td> <td valign="middle" style="border-right: 1px solid black;"><span class="bodyTxt"><?php echo $phone;?></span></td> </tr> --> <!-- <tr> <td height="44" valign="top" style="border-bottom: 1px solid black;"><div align="center"><img src="images/envelope.jpg" width="29" height="20"></div></td> <td valign="top" style="border-bottom: 1px solid black; border-right: 1px solid black;"><a href="mailto:<?php echo $get['listContactEmail'];?>" class="contactLink">Contact Us</a><br/><br/> --> <!-- new code --> <!-- <span class="bodyTxt" style="margin-left:-40px"> <?php if($get['listType'] == 'F' && $get['facebookURL'] != '') { ?> <a href="<?php echo $get['facebookURL']; ?>" target="_blank"><img src="images/facebook_icon.jpeg" title="www.facebook.com" alt="www.facebook.com" width="40" height="40" style="border-style: none"/></a> <?php } ?> <?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?> <a href="<?php echo $get['twitterURL']; ?>" target="_blank"><img src="images/twitter_icon.png" title="By: TwitterButtons.com" alt="By: TwitterButtons.com" width="125" height="40" style="border-style: none"/></a> <?php } ?> </span><br /> --> <!-- new code end --> <!-- </td> </tr> <tr> <td height="6"></td> <td></td> </tr> --> <?php } } ?> Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 15, 2010 Share Posted December 15, 2010 VERY sorry break this politeness but if you don't know how to deal with basic syntax errors I would seriously consider why you are programming at all. The syntax is the first thing you should learn. Have you ever read a book or tutorials on php? It gives you the exact line where the problem may be. I really am not saying this just to be hostile. Buy a decent php book or read up on the syntax and basic things like opening and closing brackets. Quote Link to comment Share on other sites More sharing options...
solon Posted December 15, 2010 Share Posted December 15, 2010 just remove the one of two brackets at the end of your code Quote Link to comment Share on other sites More sharing options...
adman4054 Posted December 15, 2010 Author Share Posted December 15, 2010 I did remove the bracket as the error indicated and the page wouldnt load. Sorry for the trouble. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.