Jump to content

php help in mysql row


aviel131

Recommended Posts

hey, i got mysql row with links from textarea,

i have 4 links like:

facebook.com/profile.php?id=53

facebook.com/profile.php?id=522

facebook.com/profile.php?id=664

twitter.com/profile.php?id=3434

twitter.com/profile.php?id=342

twitter.com/profile.php?id=347

 

now i want to group them to show like

 

Facebook links:

and the links of face book here

 

Twitter links:

the links of twitter here

 

how can i do this?

Link to comment
https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/
Share on other sites

something like this should work

 

<?php
$str = <<< EOS
facebook.com/profile.php?id=53
facebook.com/profile.php?id=522
facebook.com/profile.php?id=664
twitter.com/profile.php?id=3434
twitter.com/profile.php?id=342
twitter.com/profile.php?id=347
EOS;


$array = preg_split("#\n#", $str);
$output = array();
foreach ($array as $line) {
$type = preg_replace("#^(\w+)\.com.*$#","$1", $line);
if (!isset($output[$type])) $output[$type] = array();
$output[$type][] = $line;
}

foreach ($output as $name => $values) {
echo ucfirst($name) . " Links:<br />";
echo implode('<br />', $values);
echo '<br /><br />';
}

?>

This should work http://php.net/manual/en/function.parse-url.php[\url].

 

Try something like this.

<?php
$mylinks[]['link_name'] = 'http://www.facebook.com/profile.php?id=53';
$mylinks[]['link_name'] = 'http://www.facebook.com/profile.php?id=522';
$mylinks[]['link_name'] = 'http://www.facebook.com/profile.php?id=664';
$mylinks[]['link_name'] = 'http://www.twitter.com/profile.php?id=3434';
$mylinks[]['link_name'] = 'http://www.twitter.com/profile.php?id=342';
$mylinks[]['link_name'] = 'http://www.twitter.com/profile.php?id=347';
foreach($mylinks as $linki => $link) {
	$mylinks[$linki]['link_array'] = parse_url($link['link_name']);
}
print_r($mylinks);
print "\r\n\r\n";
?>

You could do something like this

 

<?php
   $row['links']['unsorted'][] = 'http://www.facebook.com/profile.php?id=53';
   $row['links']['unsorted'][] = 'http://www.facebook.com/profile.php?id=522';
   $row['links']['unsorted'][] = 'http://www.facebook.com/profile.php?id=664';
   $row['links']['unsorted'][] = 'http://www.twitter.com/profile.php?id=3434';
   $row['links']['unsorted'][] = 'http://www.twitter.com/profile.php?id=342';
   $row['links']['unsorted'][] = 'http://www.twitter.com/profile.php?id=347';
   foreach($row['links']['unsorted'] as $key => $value) {
      $row['links'][parse_url($value,PHP_URL_HOST)][] = $value;
   }
   print_r($row);
   print "\r\n\r\n";
?>

 

Which would output someting like this

 

Array
(
    [links] => Array
        (
            [unsorted] => Array
                (
                    [0] => http://www.facebook.com/profile.php?id=53
                    [1] => http://www.facebook.com/profile.php?id=522
                    [2] => http://www.facebook.com/profile.php?id=664
                    [3] => http://www.twitter.com/profile.php?id=3434
                    [4] => http://www.twitter.com/profile.php?id=342
                    [5] => http://www.twitter.com/profile.php?id=347
                )

            [www.facebook.com] => Array
                (
                    [0] => http://www.facebook.com/profile.php?id=53
                    [1] => http://www.facebook.com/profile.php?id=522
                    [2] => http://www.facebook.com/profile.php?id=664
                )

            [www.twitter.com] => Array
                (
                    [0] => http://www.twitter.com/profile.php?id=3434
                    [1] => http://www.twitter.com/profile.php?id=342
                    [2] => http://www.twitter.com/profile.php?id=347
                )

        )

)

but the links in the sql and people write them on textarea i dont know which links it can be

 

the code is:

 

                            <?php

$ap=mysql_query("SELECT * FROM downloads WHERE id='$id'");

while ($ag=mysql_fetch_array($ap)) :

$ag['links']=explode("\r",$ag['links']);

 

?>

                           

                            <?php

echo $ag['links'][0];

?>

<?php

  function sort_links($links['tmp']) {

      foreach($links['tmp'] as $key => $value) {

        $links[parse_url($value,PHP_URL_HOST)][] = $value;

      }

      unset($links['tmp']);

      return $links;

  }

  $ap = mysql_query("SELECT * FROM downloads WHERE id='$id'");

  while($ag=mysql_fetch_array($ap)) : $ag['links'] = explode("\r",$ag['links']);

  print_r(sort_links($ag['links']));

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.