Solved

array to table?

Posted on 2010-08-24
9
225 Views
Last Modified: 2012-05-10
Hi,
Please see the attached code.  I am struggle to print the array which currently prints to the web page like this:

Array ( [0] => http://www.adriennespizzabar.com/ [1] => http://www.johnsbrickovenpizza.com/ [2] => http://www.firstpizza.com/ [3] => http://www.johnspizzerianyc.com/ [4] => http://www.ottopizzeria.com/ [5] => http://www.eastvillagepizzeria.com/ [6] => http://www.joespizza.com/ ) Array ( [0] => Adrienne's Pizza Bar Restaurant [1] => John's Pizzeria of Bleecker Street [2] => Lombardi's Pizza [3] => John's Pizzeria [4] => Otto Restaurant Enoteca Pizza [5] => East Village Pizza and Kebabs [6] => Joe's Pizza )

But I want it to display in a table like this:

http://www.adriennespizzabar.com/     |  Adrienne's Pizza
http://www.johnsbrickovenpizza.com/  |  John's Pizzeria of Bleecker Street
http://www.firstpizza.com/                  |  Lombardi's Pizza

Can someone please help me with the code to achieve this?

Thanks very much
<?php

$page = file_get_contents('http://www.google.com/search?q=new+york+pizza');
 
preg_match_all(
    '@<h4[^>]*class=r[^>]*>\s*<a\s*href="(.*)".*class=l.*title="(.*)".*>@siU',
    $page,
    $matches);
 
print_r($matches[1]); // urls
print_r($matches[2]); // titles

?>

Open in new window

0
Comment
Question by:chrisj1963
  • 4
  • 3
  • 2
9 Comments
 
LVL 10

Expert Comment

by:honestman31
ID: 33517488
replace
print_r($matches[1]); // urls
print_r($matches[2]); // titles

With

foreach ($matches as $val) {
 echo  $val[1] . "|" . $val[2]  "\n";
}
0
 
LVL 10

Expert Comment

by:honestman31
ID: 33517496
replace
print_r($matches[1]); // urls
print_r($matches[2]); // titles

With

foreach ($matches as $val) {
 echo  $val[1] . "     |       " . $val[2]  "\n";
}
0
 
LVL 2

Accepted Solution

by:
dfendig earned 500 total points
ID: 33517505
Don't use print_r().

change it to this:
for($counter = 0; $counter < count($matches[1]);$counter++)
{
    echo $matches[1][$counter];
    echo '&nbsp $nbsp | &nbsp &nbsp';
    echo $matches[2][$counter];
    echo '<br />';
}

of course you could modify that code to format the data in any way, including echoing html tags to create a html table.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:chrisj1963
ID: 33517506
Thanks for the response, but it gives me a blank page result... any other thoughts on how to make it work?
thanks!
<?php

$page = file_get_contents('http://www.google.com/search?q=new+york+pizza');
 
preg_match_all(
    '@<h4[^>]*class=r[^>]*>\s*<a\s*href="(.*)".*class=l.*title="(.*)".*>@siU',
    $page,
    $matches);
 
//print_r($matches[1]); // urls
//print_r($matches[2]); // titles

foreach ($matches as $val) {
 echo  $val[1] . "     |       " . $val[2]  "\n";
}
?>

Open in new window

0
 

Author Closing Comment

by:chrisj1963
ID: 33517522
that was it. thanks very much!
0
 
LVL 10

Expert Comment

by:honestman31
ID: 33517545
sorry there is a missing dot in my code ( after 2]  )
I know it is over but please try it

foreach ($matches as $val) {
 echo  $val[1] . "     |       " . $val[2]  . "\n";
}
0
 

Author Comment

by:chrisj1963
ID: 33517567
thanks for that. The code looked much leaner, but it produces one long link:
http://www.johnsbrickovenpizza.com/ | http://www.firstpizza.com/ John&#39;s Pizzeria of Bleecker Street | Lombardi&#39;s Pizza  
0
 
LVL 2

Expert Comment

by:dfendig
ID: 33517770
"\n" doesn't display in a browser, you have to put a line break <br />
Just use honestman's cleaner code and replace the \n with the line break
0
 

Author Comment

by:chrisj1963
ID: 33517806
duh.... I've got it now.  thanks very much for your follow-up....:)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question