aviel131 Posted January 4, 2010 Share Posted January 4, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/ Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 http://ca2.php.net/substr Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/#findComment-987902 Share on other sites More sharing options...
aviel131 Posted January 4, 2010 Author Share Posted January 4, 2010 i dont think its that sample that i can use substr cuz i want to take of the http://www. for the title like: facebook.com and then display the links under the title Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/#findComment-987904 Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2010 Share Posted January 4, 2010 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 />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/#findComment-988031 Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/#findComment-988033 Share on other sites More sharing options...
aviel131 Posted January 7, 2010 Author Share Posted January 7, 2010 i now set every link in array like: $row['links'][0] is have the first link and so on... so how can i display them if i have alot of links from same host? i want display them under title of the link Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/#findComment-990564 Share on other sites More sharing options...
crabfinger Posted January 7, 2010 Share Posted January 7, 2010 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 ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/#findComment-990581 Share on other sites More sharing options...
aviel131 Posted January 7, 2010 Author Share Posted January 7, 2010 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]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/#findComment-990585 Share on other sites More sharing options...
crabfinger Posted January 7, 2010 Share Posted January 7, 2010 <?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'])); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/#findComment-990680 Share on other sites More sharing options...
Lamez Posted January 7, 2010 Share Posted January 7, 2010 Off-Topic, but all those profiles show they all graduated from Harvard. Impressive! Quote Link to comment https://forums.phpfreaks.com/topic/187076-php-help-in-mysql-row/#findComment-990682 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.