SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Explode() or preg_split() ?

    I have a TINYTEXT field in my table where some names are listed like this:

    name,email,date
    name,email,date
    etc.

    As you can see, each user's name, email, and date is seperated by a comma, and each user is seperated by a line break.

    I want to pull this from the db and place it in an array, but I can't get it to work. I started with explode() but after some searching on these forums I realized I maybe should use preg_split, but I can't get that to work either

    This is what I came up with using explode:

    PHP Code:
    $results mysql_query("SELECT users FROM table");
    while (
    $credits mysql_fetch_array($results)) {

    $up explode("<br />",$credits[users]);
    $up_lines explode(",",$up);

    $up_author $up_lines[1];
    $up_email $up_lines[2];
    $up_date $up_lines[3];

    echo 
    $up_author;

    Am I completely off track?

  2. #2
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try quoting your array index strings when they are not integers.

    In the database, is each line separated by an actual <br /> or is it a carriage return? In the case of the latter, \n may work better:
    PHP Code:
    <?php
    $results 
    mysql_query("SELECT users FROM table");
    while(
    $credits mysql_fetch_array($results)) {
         
    /*
         * try the following line with multiple variations
         * like '\n', "\n\n", '\n\n', et cetera if you aren't
         * explicitly using <br /> in your database data
         */
         
    $up explode("\n",$credits['users']);
         
    $up_lines explode(",",$up);
         
    $up_author $up_lines[1];
         
    $up_email $up_lines[2];
         
    $up_date $up_lines[3];
         echo 
    $up_author;
    }
    ?>
    Last edited by Aes; Mar 15, 2002 at 17:17.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  3. #3
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $results mysql_query("SELECT users FROM table");
    while (
    $credits mysql_fetch_array($results)) {

    $up explode("\n",$credits[users]);  // seperate by newline
    foreach $up as $up_lines
    {
      
    $up_lines explode(",",$up);  // separate by ,

      
    $up_author $up_lines[0];
      
    $up_email $up_lines[1];
      
    $up_date $up_lines[2];

      echo 
    $up_author;

    each field has several of these lines separated by newlines so when you do this:
    PHP Code:
    $up explode("\n",$credits[users]); 
    $up is an array of lines of name,email,date... now for each of these lines in the array, you've to further explode them lines again...

    <<edit>> your array indices should also start from 0
    Last edited by redemption; Mar 15, 2002 at 21:09.

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the explanation of how to use explode(). It now makes sense to me and the query almost works

    The only problem is that it echoes "Array" instead of the username. It does, however, echo Array according to the number of names, meaning that if there are 3 names, it echoes Array 3 times.

  5. #5
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    did you explode() twice like i said?

  6. #6
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you store each bit (name, email etc) in a seperate field in the database? that way you can just use mysql_fetch_array() for your query results.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  7. #7
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Defender1
    why don't you store each bit (name, email etc) in a seperate field in the database? that way you can just use mysql_fetch_array() for your query results.
    Because the number of users is different for each row.

    did you explode() twice like i said?
    Yes, I use the exact code as you posted it

  8. #8
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops my bad i had a stoopid error in my code up above... notice the new line
    PHP Code:
    $up_lines explode(",",$up_lines); 
    the fixed code:
    PHP Code:
    $results mysql_query("SELECT users FROM table");
    while (
    $credits mysql_fetch_array($results)) {

    $up explode("\n",$credits[users]);  // seperate by newline
    foreach $up as $up_lines
    {
      
    $up_lines explode(",",$up_lines);  // separate by ,

      
    $up_author $up_lines[0];
      
    $up_email $up_lines[1];
      
    $up_date $up_lines[2];

      echo 
    $up_author;

    hope this works... sorry about the error

  9. #9
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that did the trick!

    You don't need to apologize. I'm just happy you want to help me

    Thanks for the help!


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
  •