GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP guys (or gals!) - quick question (https://gfy.com/showthread.php?t=930497)

thunder99 09-29-2009 04:26 AM

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?

fris 09-29-2009 04:39 AM

http://www.php.net/explode

StariaDaniel 09-29-2009 04:42 AM

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'
);

frank7799 09-29-2009 04:46 AM

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++;
     } 


CurrentlySober 09-29-2009 05:20 AM

U smell of wee-wee

irbobo 09-29-2009 05:34 AM

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
        )

)


irbobo 09-29-2009 05:40 AM

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 />";
        }
?>


irbobo 09-29-2009 05:42 AM

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.

thunder99 09-29-2009 08:06 AM

Thanks fris, StariaDaniel, m4yadult & irbobo. That works!

irbobo 09-29-2009 08:30 AM

just procrastinating

gornyhuy 09-29-2009 08:56 AM

Quote:

Originally Posted by irbobo (Post 16370327)
just procrastinating

Same here. Fuck but I'm unmotivated and over tasked today.


All times are GMT -7. The time now is 11:09 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc