Solved

array to table?

Posted on 2010-08-24
9
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

735 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