SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: Array problems!

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array problems!

    Hi all, I'm having terrible trouble at the moment with arrays and would really appreciate your assistance. My database has several tables, two of which include:

    booksread_tbl (records the books that each member read)- details put into an array called $booksread

    userid|bookid
    1|1
    2|3
    3|3
    2|2
    etc|etc

    books_tbl (records the name and id of each book) - details put into an array called $books

    bookid|bookname
    1|Football legends
    2|The Bible
    3|The SQL Guide
    etc|etc

    I also have an array called $member with contains a list of 'selected' members userids (1,3,etc) - It's not the full member list.

    The script that I have, and which I can't really change, uses a 'while' statement to list out each bookid and name contained in the database in alphabethical order.

    I now want to use a while statement or a for each statement to put the userids of my selected members, who have read that particular book, beside the book name.

    Thanks for this - I've put hours into it but feel that I should have gone fishing or something!

    Willie

  2. #2
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried a JOIN query?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that a Join would work. I can't interfere with the $books query as it's being used for another purpose and to run a second query with a join would be resouce intensive. I would prefer to focus on $booksread and $members and to loop through these arrays somehow while each book is in focus so to speak.

    I also searched the forums here for info on repeatedly running foreach and while statements on arrays but didn't get too far. My while and foreach statements seem to iterate an array once and no more, therefore when I come to the second book in the list, the iteration doesn't appear to run.

    Thanks for the reply.

    Willie

  4. #4
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please show us the script you have put together so far.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply and for taking an interest. However, I think that posting the script here would totally confuse my query above - there are too many variables, queries, joins etc in the script (it's a third party script I am using with consent). This only applies to the opening while loop that would loop through all the bookids and titles. Thereafter, I really have no script because I don't know what to do. What I have done in my first post is to simplify what I want to do. Once I achieve that, then I can expand and amend it to suit my needs.

    Willie

  6. #6
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm..still a bit hard to help at least for me without no code. :/

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the essence of my script. I think you will find that it is obvious I need a bit of help!!

    $books = mysql_query("SELECT * FROM books_tbl" );
    if (!$books) {
    echo("Unable to retrieve list of books: " . mysql_error() . "" );
    exit();
    }

    $booksread = @mysql_query("SELECT * FROM booksread_tbl WHERE userid IN $members" );
    if (!$booksread) {
    echo("Unable to retrieve list of books read by selected members: " . mysql_error() . "" );
    exit();
    }

    WHILE ( $bookslist = MYSQL_FETCH_ARRAY ( $books ) ) {

    //reset the read this book list array
    unset($readthisbook);

    // Determine if my selected members read some books
    if (mysql_num_rows($booksread)>0){
    while ( $row = mysql_fetch_array($booksread) ) {

    if ($row["bookid"]==$bookslist[bookid]) {
    //a selected member read this book therefore record his userid
    $readthisbook[]=$row[userid];


    // here the name of the book is printed along with the userids of selected members who read it.

    ?>
    <?=$bookslist[bookid] $bookslist[bookname]?> Read by: <?$readthisbook?>

    <?
    } // end of if $row
    } // end of while $row
    } // end of if mysql_num_rows

    }//end of while $books

  8. #8
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay I see and what is the exact problem then ? Only wanting to put the userid besides the books name ?

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's exactly what I need!

  10. #10
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Huh ? *confused*

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That make's two of us then!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •