September 6, 200719 yr Hi All im trying to get this bit of code to work, I need 'if an image is set, display, if not, view without image'. At present I get the image trying to view, giving me the surrounding image size which i do not want? <?php $page_title = 'view'; require_once ('../../mysql_connect.php'); // Connect to the db. $imagesize='height="150" width="150"'; $alignright='right'; /// selected everything from db $query = "SELECT * FROM newspage"; $result = @mysql_query ($query); $num = mysql_num_rows($result); if ($num > 0) { // If num echo '<a name="top"></a>'; echo '<h3>news</h3>'; echo '<p> </p>'; while ($myrow = mysql_fetch_array($result, MYSQL_ASSOC)) { //While $newsText=(htmlspecialchars(strip_tags($myrow['news']))); echo '<br /> '; echo '<p class="bold" align="left">' .$myrow['heading'] .'</p>'; /// display image if one set if($myrow['file1']){ echo '<img ' . $imagesize .' src="uploads/'.$myrow['file1'.$file] .'" align="'.$alignright .'" />'; } else { echo '<p>'.$myrow['news'].'</p>'; } echo '<p><a href="top" class="style1">Back to Top</a></p>'; echo '<br /> '; echo '<hr />'; } //End of if num } //End of while else { /// if db not run display error echo '<table align="center" cellspacing="0" cellpadding="5">'; echo '<td><span class = "style1">Sorry, no records found!</span></td>'; echo '</table>'; } ?> hope it makes sense! Your help is much appreciated!
September 7, 200719 yr Author Ive even tried file exist??? even tried using file exist? <?php $page_title = 'view'; require_once ('../../mysql_connect.php'); // Connect to the db. $imagesize='height="150" width="150"'; $alignright='right'; /// selected everything from db $query = "SELECT * FROM newspage"; $result = @mysql_query ($query); $num = mysql_num_rows($result); if ($num > 0) { // If num echo '<a name="top"></a>'; echo '<h3>news</h3>'; echo '<p> </p>'; while ($myrow = mysql_fetch_array($result, MYSQL_ASSOC)) { //While $newsText=(htmlspecialchars(strip_tags($myrow['news']))); echo '<br /> '; echo '<p class="bold" align="left">' .$myrow['heading'] .'</p>'; /// display image if one set if (file_exists($myrow['file1'])){ echo '<img ' . $imagesize .' src="uploads/'.$myrow['file1'.$file] .'" align="'.$alignright .'" />'; } else { echo '<p>'.$myrow['news'].'</p>'; } echo '<p><a href="top" class="style1">Back to Top</a></p>'; echo '<br /> '; echo '<hr />'; } //End of if num } //End of while else { /// if db not run display error echo '<table align="center" cellspacing="0" cellpadding="5">'; echo '<td><span class = "style1">Sorry, no records found!</span></td>'; echo '</table>'; } ?>
September 7, 200719 yr file_exists is the function you should use. However, what does the variable $myrow['file1'] contain? Are you sure it points to the correct local path?
September 7, 200719 yr first you really should alter that code. replace fetch_array($result, 'MYSQL_ASSOC') with fetch_assoc($result); Also NEVER use select *. Always select the fields. it's just better practise and stops you pulling data you don't need. Also you are checking if file1 exists but then displaying $myrow['file1'.$file]? is that an error or what are you trying to do? Lastly what exactly is the system doing wrong? what do you mean giving you the surrounding image size?
September 7, 200719 yr Also NEVER use select *. Always select the fields. it's just better practise and stops you pulling data you don't need. Even if you actually want all the fields?
September 7, 200719 yr Even if you actually want all the fields? saves you altering the code later if you alter the database.
September 7, 200719 yr Author Hi Thanks for the tips I made a few changes... Why should I use mysql_fetch_asoc ? <?php $page_title = 'view'; require_once ('../../mysql_connect.php'); // Connect to the db. $imagesize='height="150" width="150"'; $alignright='right'; /// selected everything from db $query = "SELECT heading, news, file1 FROM newspage"; $result = @mysql_query ($query); $num = mysql_num_rows($result); if ($num > 0) { // If num echo '<a name="top"></a>'; echo '<h3>news</h3>'; echo '<p> </p>'; while ($myrow = mysql_fetch_assoc($result)); { //While echo '<br /> '; echo '<p class="bold" align="left">' .$myrow['heading'] .'</p>'; /// display image if one set if (file_exists($myrow['file1'])){ echo '<img ' . $imagesize .' src="uploads/'.$myrow['file1'] .'" align="'.$alignright .'" />'; } else { echo '<p>'.$myrow['news'].'</p>'; } echo '<p><a href="top" class="style1">Back to Top</a></p>'; echo '<br /> '; echo '<hr />'; } //End of if num } //End of while else { /// if db not run display error echo '<table align="center" cellspacing="0" cellpadding="5">'; echo '<td><span class = "style1">Sorry, no records found!</span></td>'; echo '</table>'; } ?> What im trying to do is, check if image prensent (file1), if there show it, if not, skip and show news? Ive attached a screen shot.
September 7, 200719 yr fetch assoc is just the new way of doing it. fetch array is an old form unless you actually want it as an array. i can't see the screen shot?
September 9, 200719 yr Author Hi Im trying to check if a file exists in 'uploads folder' and check to see if 'file1' has any information in. if I didnt upload a file (have the option to upload image or not), so the file1 data for that 'id' has a empty field, I want to display that information backout but check to see if that field is empty or not as sometimes there could be an image present? But still checking if there was a file on the server too.. Ive got this so far, it just displays the $news, when there is file on the server? Hope this makes sense? <code> <?php $page_title = 'viewnews'; require_once ('../mysql_connect.php'); // Connect to the db. $imagesize='height="100" width="100"'; $alignright='right'; /// selected everything from db $query = "SELECT * FROM newspage"; $result = @mysql_query ($query); $num = mysql_num_rows($result); if ($num > 0) { // If num echo '<a name="top"></a>'; echo '<div id="maincentredbig">Latest news</div> '; echo '<br /> '; while ($myrow = mysql_fetch_array($result, MYSQL_ASSOC)) { //While $newsText=(htmlspecialchars(strip_tags($myrow['news']))); $firstImage='uploads/'.$myrow['file1'].'.jpg'; $file1db=$myrow['file1']; echo '<br /> '; echo'<p class="bold" align="left">' .$myrow['heading'] .'</p>'; if(file_exists($firstImage)){ echo '<p class="alertHd" align="left">' .$myrow['news'].'<img ' . $imagesize .' src="uploads/'.$myrow['file1'].'" align="'.$alignright .'"/></p>'; } else if (empty($file1db)){ echo'<p class="alertHd" align="left">' .$myrow['news'].'</p>'; } else { echo'<p class="alertHd" align="left">' .$myrow['news'].'</p>'; } echo'<a href="#top">Back to Top</a>'; echo '<br /> '; echo'<hr />'; } //End of if num } //End of while else { /// if db not run display error echo '<table align="center" cellspacing="0" cellpadding="5">'; echo'<td><span class = "style1">Sorry, no records found!</span></td>'; echo '</table>'; } ?> </code>
September 9, 200719 yr why are you checking to see if the field is empty after you check for the file? I cleaned up the code a bit so I could read it better but I fail to see what problem it could cause. can you produce a screen shot of what it should look like and what it does look like? <?php $page_title = 'viewnews'; require_once ('../mysql_connect.php'); // Connect to the db. $imagesize='height="100" width="100"'; $alignright='right'; /// selected everything from db $query = "SELECT file1, news, heading FROM newspage"; $result = @mysql_query ($query); $num = mysql_num_rows($result); if ($num > 0) { // If num echo '<a name="top"></a>'; echo '<div id="maincentredbig">Latest news</div> '; echo '<br /> '; while ($myrow = mysql_fetch_assoc($result)) { //While $newsText=(htmlspecialchars(strip_tags($myrow['news']))); $firstImage='uploads/'.$myrow['file1'].'.jpg'; $file1db=$myrow['file1']; echo '<br /> '; echo'<p class="bold" align="left">' .$myrow['heading'] .'</p>'; if (empty($file1db)){ echo'<p class="alertHd" align="left">' .$myrow['news'].'</p>'; } else if(file_exists($firstImage)){ echo '<p class="alertHd" align="left">' .$myrow['news'].'<img ' . $imagesize .' src="uploads/'.$myrow['file1'].'" align="'.$alignright .'"/></p>'; } else { echo'<p class="alertHd" align="left">' .$myrow['news'].'</p>'; } echo'<a href="#top">Back to Top</a>'; echo '<br /> '; echo'<hr />'; } //End of while } //End of if num else { /// if db not run display error echo '<table align="center" cellspacing="0" cellpadding="5">'; echo'<td><span class = "style1">Sorry, no records found!</span></td>'; echo '</table>'; } ?>
September 10, 200719 yr Author Thanks... What I was trying to achieve was <code> if(!empty($myrow['file1'])){ if(file_exists($firstImage)){ echo '<p class="alertHd" align="left"><img ' . $imagesize .' src="uploads/'.$myrow['file1'] .'" align="'.$alignright .'"/>'.$myrow['news'].'</p>'; } } else { echo'<p class="alertHd" align="left">'.$myrow['news'].'</p>'; } </code>
Create an account or sign in to comment