0

hi everyone....

i have a script i made that chooses a random name from a database
i need to output many names on the page the code is used on, one name for each comment shown on screen.....i tried a few things but the code below is resulting 10 names per message instead of 1 (the 10 is set as my limit in the code made to select a random name)

heres the code in the view (i code with codeigniter)

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines


<?php foreach($comments->result() as $com)
    {
 foreach($whatsmyname->result() as $whatsmy)
{
    echo '<p>'.$whatsmy->name.' commented on '.$com->bid_time.'</p><p>'.$com->bid_desc.'</p>';?></br>
    <?php }}?>

what i need is to maybe find a way of limiting the names to the ammount of comments posted? im not sure how i would do that?

im open to any ideas...thanks

4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by diafol
0

Are you putting the result names into an array? To do this, use a while loop for your MySQL result, like this:

    while($row = mysqli_fetch_array($result)){
        $name[] = $row['name'];
    }

Edited by elbeato: missing brackets

0

need to know what

$comments->result() returns 

and what

$whatsmyname->result() returns

im guessing there shouldnt be 2 foreach loops it should be more like:

<?php
function getUserNames(){
    $array = array(1=>'Bim',2=>'Bob',3=>'Schlokka',4=>'Elbeato');
    return $array;
}
$names = getUserNames(); 
foreach($comments->result() as $com){
    echo '<p>'.$names[$com->username_id].' commented on '.$com->bid_time.'</p><p>'.$com->bid_desc.'</p><br/>';
}
?>

From the sound of it you might want to tell us the mysql query too and what its supposed to pull

Edited by Biiim: extra comment

0

hi, yes i agree there shouldnt be 2 loops, what i need is something that will output the names with the condition of how many comments there are!

heres the sql/model

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

function getfakename($conditions=array())
{

        $result = $this->db->query("SELECT distinct(name) FROM names ORDER BY RAND() LIMIT 0,10");  


    return $result;


}   




Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

comments->result returns all comments made for whatever thread the user is reading

whatsmyname results 10 names randomly selected from a database of about 100

what i want is for a random name to appear by the side of each comment shown on screen.

thanks

1

I'm a little confused as to why you'd want random names next to comments. I'll do it in vanilla procedural for now - you can OOP it yourself.

 $result = mysql_query("SELECT distinct(`name`) FROM `names`");
 while($d = mysql_fetch_array($result)){
    $names[] = $d['name'];
 }

 $max = count($names)-1;
 result = mysql_query("SELECT * FROM `comments`");
 while($d = mysql_fetch_array($result)){
    $n = $names[mt_rand(0,$max)];
    echo '<p>'.$n.' commented on '.$d['bid_time'].'</p><p>'.$d['bid_desc'].'</p><br/>';
 }

Or have I misunderstood?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.