Hopefully someone can correct this code for me. It Should be using <?=$records[1]?> PHP: on the main index to pull a random article from the specified category using <?php include "conn.php"; $categories = array('Arts', 'Business', 'Computers', 'Health', 'Home', 'Kids_And_Teens', 'Recreation', 'Reference', 'Science', 'Shopping', 'Society','Sports'); $records = array(); for($i=0;$i <=11;$i++){ $sql ="SELECT * FROM articles WHERE category = '$categories[$i]' ORDER BY id"; $rs = mysql_query($sql, $db) or die (mysql_error()); $limitvalue = round(0,mysql_numrows($rs)-1); $sq ="SELECT * FROM articles WHERE category = '$categories[$i]' LIMIT $limitvalue,1"; $rst = mysql_query($sq, $db) or die (mysql_error()); $datas = mysql_fetch_array($rst); $desc = substr($datas[article],0,strpos($datas[article],".")); $records[$i] = "<font size = 2 face = verdana> <a href = 'http://www.articledepot.co.uk/displayarticle.php?id=$datas[id]' title ='$datas[title]'><b>$datas[title] </b></a> - <font size = 2>$desc</font> <p>"; } ?> PHP: Only it seems to just be pulling the lowest id - Can anyone lend a hand to correct the mistake that is keeping it from pulling a random article? You can see it in action on Article Depot Thanks in advance
I read through that T0PS (and thank you for it) but I am not the best PHP person in the world so I will change this thread a bit: $4 if you can provide cut and paste code that will display the last submitted (biggest id) article from each category - all the details you need should be in the original post OR $6 if you can make the original code work (or swrite your own code from scratch) and have a random article display for each category in the array Payment will be via Paypal and it MUST be cut and paste complete code. Tips to put me in the right direction will be appreciated but won't get you paid. See my other thread to make a few more bucks in just a few minutes: http://forums.digitalpoint.com/showthread.php?t=15818
I don't know whether this will work perfectly - I've not tested it at all. <?php include "conn.php"; $categories = array('Arts', 'Business', 'Computers', 'Health', 'Home', 'Kids_And_Teens', 'Recreation', 'Reference', 'Science', 'Shopping', 'Society','Sports'); $records = array(); $a=0; foreach ($categories as $cat){ $sql = 'SELECT * FROM articles WHERE category = "'.$cat.'"; //find number of articlesetch an array of all articles $rs = mysql_query($sql, $db) or die (mysql_error()); $i=0; while ($data = mysql_fetch_array($rs)){ $articles[$i] = $data; $i++; } //count number of articles fetched $numArticles = count($articles); //select and article to display $randArticle = rand(0,$numArticles-1); $ArticleToDisplay = $articles[$randArticle]; $desc = substr($ArticleToDisplay[article],0,strpos($ArticleToDisplay[article],".")); $records[$a] = '<font size = 2 face = verdana> <a href = "http://www.articledepot.co.uk/displayarticle.php?id='.$ArticleToDisplay[id].'" title ="'.$ArticleToDisplay[title].'"><b>'.$ArticleToDisplay[title].' </b></a> - <font size = 2>'.$desc.'</font> <p>'; $a++; }
That code gives: Parse error: parse error, unexpected T_STRING in /home/content/a/r/t/articlesjtr/html/randomarticle.php on line 27 Also, make sure you use the php tags when posting code so it doesn't get changed (highlight the code and hit the piece of paper with PHP on it) Cheers
Line 27 is the $records[$a]= bit. Looks like it split across a couple of lines when I pasted it. <?php //include "conn.php"; $categories = array('Arts', 'Business', 'Computers', 'Health', 'Home', 'Kids_And_Teens','Recreation', 'Reference', 'Science', 'Shopping', 'Society','Sports'); $records = array(); $a=0; foreach ($categories as $cat){ $articles=array(); $sql = 'SELECT * FROM articles WHERE category = "'.$cat.'"'; //find number of articlesetch an array of all articles $rs = mysql_query($sql, $db) or die (mysql_error()); $i=0; while ($data = mysql_fetch_array($rs)){ $articles[$i] = $data; $i++; } if ($articles[0]!=""){ //count number of articles fetched $numArticles = count($articles); //select and article to display $randArticle = rand(0,$numArticles-1); $ArticleToDisplay = $articles[$randArticle]; $desc = substr($ArticleToDisplay[article],0,strpos($ArticleToDisplay[article],".")); $records[$a] = '<font size = 2 face = verdana><a href = "http://www.articledepot.co.uk/displayarticle.php?id='.$ArticleToDisplay[id].'" title ="'.$ArticleToDisplay[title].'"><b>'.$ArticleToDisplay[title].' </b></a> - <font size = 2>'.$desc.'</font> <p>'; $a++; } } ?> PHP: You might want to check that $ArticleToDisplay is actually an array. Comment out line 27 and add a print_r($ArticleToDisplay)
If anyone is still looking to persue this for the $6, you might be able to get some clues here: www. php-forum.com/p/viewtopic.php?p=24522 They started to go into how you should write the code yourself which is usually where they lose me. I am cut and paste for this project Good Luck
We have a winner. JLawrence came through. Make sure to give him a few green reps from me as this guy is really good. DP really does make dreams come true (Ok, now I'm pushing it)
OK, post #7 has been edited so that it's got the code that worked in it. Just incase it's of use to anyone else.