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

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'];
    }

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

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

Member Avatar for diafol

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.