1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Pulling a Random Result

Discussion in 'PHP' started by yfs1, May 10, 2005.

  1. #1
    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
     
    yfs1, May 10, 2005 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    T0PS3O, May 10, 2005 IP
  3. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    yfs1, May 11, 2005 IP
  4. jlawrence

    jlawrence Peon

    Messages:
    1,368
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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++;
    }
     
    jlawrence, May 11, 2005 IP
    palespyder likes this.
  5. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
     
    yfs1, May 11, 2005 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Which one is line 27?
     
    T0PS3O, May 11, 2005 IP
  7. jlawrence

    jlawrence Peon

    Messages:
    1,368
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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)
     
    jlawrence, May 11, 2005 IP
    yfs1 likes this.
  8. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It still produces that same error J
     
    yfs1, May 11, 2005 IP
  9. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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
     
    yfs1, May 11, 2005 IP
  10. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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)
     
    yfs1, May 11, 2005 IP
  11. jlawrence

    jlawrence Peon

    Messages:
    1,368
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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.
     
    jlawrence, May 11, 2005 IP