spikypunker Posted October 23, 2009 Share Posted October 23, 2009 Hi guys, am slowly finding my way into the php world. I've managed to build an events sidebar, that displays 4 entries from the events database and then links to a page displaying details. The only problem is, currently the code goes haywire if there's not 4 entries in the database? Is there a simple fix? <?php mysql_connect ("#","#"); @mysql_select_db("#") or die ("unable to connect"); $query = "SELECT * FROM events ORDER BY date DESC "; $result = mysql_query($query); $num = mysql_num_rows($result); mysql_close(); $i = 0; while ($i < 4) { $date = mysql_result($result,$i,"date"); $title = mysql_result($result,$i,"title"); $id = mysql_result($result,$i,"id"); if(strlen($title) > 20){ $title = substr($title,0,20).'...';} print date("d.m.Y",strtotime("$date")); echo " <a href='eventitem.php?id=$id'>$title</a><br>"; $i++; } ?> Cheers dudes! Chris Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/ Share on other sites More sharing options...
Bricktop Posted October 23, 2009 Share Posted October 23, 2009 Hi Chris, Use the MySQL LIMIT function rather than use a PHP while loop. Amended code below: <?php mysql_connect ("#","#"); @mysql_select_db("#") or die ("unable to connect"); $query = "SELECT * FROM events ORDER BY date DESC LIMIT4"; $result = mysql_query($query); mysql_close(); while ($a = mysql_fetch_array($query)) { $date = $a['date']; $title = $a['title']; $id = $a['id']; if(strlen($title) > 20){ $title = substr($title,0,20).'...';} print date("d.m.Y",strtotime("$date")); echo " <a href='eventitem.php?id=$id'>$title</a><br>"; } ?> Have a look here for more information on MySQL LIMIT. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/#findComment-942737 Share on other sites More sharing options...
spikypunker Posted October 23, 2009 Author Share Posted October 23, 2009 Ok cool so i need to use an array? Hmm i really need to do some reading up on them. Cheers for helping amend the code, the only thing is, i also need to limit it to 4! :/ Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/#findComment-942746 Share on other sites More sharing options...
spikypunker Posted October 23, 2009 Author Share Posted October 23, 2009 This right? <?php mysql_connect ("#","#"); @mysql_select_db("#") or die ("unable to connect"); $query = "SELECT * FROM events ORDER BY date DESC LIMIT4"; $result = mysql_query($query); mysql_close(); while ($a = mysql_fetch_array($query) && $ < 4) { $date = $a['date']; $title = $a['title']; $id = $a['id']; if(strlen($title) > 20){ $title = substr($title,0,20).'...';} print date("d.m.Y",strtotime("$date")); echo " <a href='eventitem.php?id=$id'>$title</a><br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/#findComment-942750 Share on other sites More sharing options...
kickstart Posted October 23, 2009 Share Posted October 23, 2009 Hi Need a space between the limit and the 4. And no point in trying to check the count in the while loop <?php mysql_connect ("#","#"); @mysql_select_db("#") or die ("unable to connect"); $query = "SELECT * FROM events ORDER BY date DESC LIMIT 4"; $result = mysql_query($query); mysql_close(); while ($a = mysql_fetch_array($query)) { $date = $a['date']; $title = $a['title']; $id = $a['id']; if(strlen($title) > 20) { $title = substr($title,0,20).'...'; } print date("d.m.Y",strtotime("$date")); echo " <a href='eventitem.php?id=$id'>$title</a><br>"; } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/#findComment-942754 Share on other sites More sharing options...
Bricktop Posted October 23, 2009 Share Posted October 23, 2009 Whoops! Sorry missed the space between LIMIT and 4! Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/#findComment-942765 Share on other sites More sharing options...
spikypunker Posted October 23, 2009 Author Share Posted October 23, 2009 AHHHH sorry i'd totally missed what you'd done, LIMIT cool beans! Cheers for that Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/#findComment-942774 Share on other sites More sharing options...
spikypunker Posted October 23, 2009 Author Share Posted October 23, 2009 I'm getting an error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites/123reg/LinuxPackage21/th/eb/oo/thebookhive.co.uk/public_html/aboutus.php on line 72 Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/#findComment-942807 Share on other sites More sharing options...
Bricktop Posted October 23, 2009 Share Posted October 23, 2009 Hi spikypunker, try: <?php mysql_connect ("#","#"); @mysql_select_db("#") or die ("unable to connect"); $query = mysql_query("SELECT * FROM events ORDER BY date DESC LIMIT 4"); while ($a = mysql_fetch_array($query)) { $date = $a['date']; $title = $a['title']; $id = $a['id']; if(strlen($title) > 20){ $title = substr($title,0,20).'...';} print date("d.m.Y",strtotime("$date")); echo " <a href='eventitem.php?id=$id'>$title</a><br>"; } ?> I've removed both mysql_close() and the $result= line and amended the MySQL Query. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/#findComment-942813 Share on other sites More sharing options...
spikypunker Posted October 23, 2009 Author Share Posted October 23, 2009 Woop! Cheers man works great! A new thing learnt! LIMIT = GENIUS! Quote Link to comment https://forums.phpfreaks.com/topic/178720-solved-problem-if-theres-not-enough-entries-in-the-database-simple-if-you-know-how/#findComment-942848 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.