PHP guys (or gals!) - quick question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thunder99
    Confirmed User
    • Nov 2003
    • 503

    #1

    PHP guys (or gals!) - quick question

    I have data in a mysql table in the format:

    description title:description text;description title2:description text2;
    etc

    Is there any way I put this into an array (or something simillar) to print it out and format it nicely?
    yeah, yeah
  • fris
    Too lazy to set a custom title
    • Aug 2002
    • 55679

    #2
    http://www.php.net/explode
    Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

    Comment

    • StariaDaniel
      Confirmed User
      • Oct 2007
      • 415

      #3
      first explode the string on every ;, next explode every array element on :

      http://de.php.net/manual/en/function.explode.php

      e.g.:

      $tmp = 'description title:description text;description title2:description text2';
      $tmp2 = explode(';',$tmp);

      returns:

      $tmp2[0] = 'description title:description text';
      $tmp2[1] = 'description title2:description text2';

      now you could do something like:
      $tmp3 = array();
      foreach($tmp2 AS $value){
      $tmp3[] = explode(':',$value);
      }

      and you get

      $tmp3[0] = array(
      0 => 'description title',
      1 => 'description text'
      );
      $tmp3[1] = array(
      0 => 'description title2',
      1 => 'description text2'
      );
      YezzClips.com - Sell your clips and photosets - Promote YezzClips Clipstores and get 20% revshare for life
      YooGirls.com - Sell your non-nude content - Promote Yoogirls Clipstores and get 20% revshare for life
      [email protected] • ICQ #464114379

      Comment

      • frank7799
        Confirmed User
        • Jul 2003
        • 1974

        #4
        You can use a query and put the result into an array. There is an example below. You will need html where the variables are integrated in the "while" loop.


        PHP Code:
        $query = "select description,title from ".MYSQLTABLE." where (site = '$site') $order";
        $result = mysql_query($query);
        
            $number = mysql_numrows($result);
            
            $i = 1;
            while ($i <= $number)
            {
         
              $row   = mysql_fetch_array($result);
              $title = stripslashes($row["title"]);
              $description = stripslashes(nl2br($row["description"]));
        
              $i++;
             } 
        

        Comment

        • CurrentlySober
          Too lazy to wipe my ass
          • Aug 2002
          • 38943

          #5
          U smell of wee-wee


          👁️ 👍️ 💩

          Comment

          • irbobo
            Confirmed User
            • Dec 2005
            • 410

            #6
            PHP Code:
            <?
            $str = "description title:description text;description title2:description text2;";
            
            function data_to_array($str)
            {
                foreach( explode(';',$str) as $value )
                    if( !empty($value) && strstr($value, ':') )
                        $newarray[] =  array( 'title'=>current(explode(':', $value)), 'description'=>end(explode(':', $value)) );
                    
                return $newarray;
            }
            
            print_r(data_to_array($str));
            ?>
            output is:

            Code:
            Array
            (
                [0] => Array
                    (
                        [title] => description title
                        [description] => description text
                    )
            
                [1] => Array
                    (
                        [title] => description title2
                        [description] => description text2
                    )
            
            )
            Last edited by irbobo; 09-29-2009, 04:36 AM.

            Comment

            • irbobo
              Confirmed User
              • Dec 2005
              • 410

              #7
              in reference to my above post:

              Code:
              <?
              	$mydata = data_to_array($str);
              	
              	foreach($mydata as $data)
              	{
              		echo $data['title']."<br />";
              		echo $data['description']."<br />";
              		echo "<hr />";
              	}
              ?>

              Comment

              • irbobo
                Confirmed User
                • Dec 2005
                • 410

                #8
                another thing if your PHP is set to super strict you may want to return $newarray as @$newarray to avoid tripping a warning if there is no data sent to the function.

                Comment

                • thunder99
                  Confirmed User
                  • Nov 2003
                  • 503

                  #9
                  Thanks fris, StariaDaniel, m4yadult & irbobo. That works!
                  yeah, yeah

                  Comment

                  • irbobo
                    Confirmed User
                    • Dec 2005
                    • 410

                    #10
                    just procrastinating

                    Comment

                    • gornyhuy
                      Chafed.
                      • May 2002
                      • 18041

                      #11
                      Originally posted by irbobo
                      just procrastinating
                      Same here. Fuck but I'm unmotivated and over tasked today.

                      icq:159548293

                      Comment

                      Working...