Solved

I need help with a PHP page collector

Posted on 2011-02-18
11
281 Views
Last Modified: 2012-05-11
I have the following code and what it does is collect pages listed in a directory and display them on the page.

Basically a catalog generator.
I want to modify it and have it gather other information. Mostly a item title and picture.
Anyone know how I would modify this code to do that?
<?php
	$files = glob("*.html");
	$ignore_files = array( //add any file names of files you don't want to appear in the site map
		"formsubmit.php",
		"thankyou.html",
		"_vti_inf.html",
		"postinfo.html",
	);
	foreach($files as $f){ 
		if(in_array($f,$ignore_files))continue;
		$file_data = file_get_contents($f);
		if(preg_match('/<title>([^<]+)<\/title>/i',$file_data,$matches)){
		$page_title = $matches[1];
		}else{
		$page_title = $f;
		}
		
		?>
                <li><a href="<?=$f;?>">
                  <?=$page_title;?>
                  </a></li>
                <?
	}
	?>

Open in new window

0
Comment
Question by:rodneygray
  • 6
  • 4
11 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 34929133
First you'd have to know where you get this new information from. Are those file all raster images and/or do you have the item titles or pictures stored somewhere?

0
 
LVL 1

Author Comment

by:rodneygray
ID: 34929366
I am storing the item names between <!--<item-name>Pretend Item</item-name>-->.
As of images I would put <!--<productimage>--><img src="../images/img.jpg"/><!--</productimage>-->
0
 
LVL 1

Author Comment

by:rodneygray
ID: 34929746
As far as the picture goes I just need it to pull the URL information and display the picture. I don't think it can copy the full thing, maybe I'm wrong.

But I would like it to display the page name, an item name, then an item picture.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:onemadeye
ID: 34929827
Okay, simply you can continue the preg_match pattern from your codes to also display other information on the HTML page.

Here's a solution for you...
Assumed the HTML page looks like this.
Pay attention to this pattern :
<item-name>Item Name Here</item-name>
<item-price>Item Price Here</item-price>
<item-image><img src="item-image.jpg" /></item-image>
<html>
<head>
<title> Monster Truck Nitro </title>
</head>
<body>
<b>PRODUCT DETAILS:</b> <br />
Item Name: <item-name>Monster Truck Nitro</item-name><br />
Item Price: <item-price>USD 2500</item-price><br />
Item Image: <item-image><img src="item-image.jpg" /></item-image><br />
</body>
</html>

Open in new window


And here's your modified PHP code:
<?php
	$files = glob("*.html");
	$ignore_files = array( //add any file names of files you don't want to appear in the site map
		"formsubmit.php",
		"thankyou.html",
		"_vti_inf.html",
		"postinfo.html",
	);

	foreach($files as $f){ 
		if(in_array($f,$ignore_files)) continue;
		$file_data = file_get_contents($f);

		if(preg_match('/<title>([^<]+)<\/title>/i',$file_data,$matches)){
		$page_title = $matches[1];
		}else{
		$page_title = $f;
		}

		if(preg_match('/<item-name>([^<]+)<\/item-name>/i',$file_data,$matches)){
		$item_name = $matches[1];
		}else{
		$item_name = 'N/A';
		}

		if(preg_match('/<item-price>([^<]+)<\/item-price>/i',$file_data,$matches)){
		$item_price = $matches[1];
		}else{
		$item_price = 'N/A';
		}

		// different preg_match pattern used
		if(preg_match('/<item-image>*(.*?)\<\/item-image>/i',$file_data,$matches)){
		$item_image = $matches[1];
		}else{
		$item_image = 'N/A';
		}
		
?>
<li>
<a href="<?=$f;?>"><?=$page_title;?></a><br />
Item Name: <?=$item_name?><br />
Item Price: <?=$item_price?><br />
Item Image: <?=$item_image?><br />
</li>
<?php
	} // END foreach
?>

Open in new window


Let me know...
0
 
LVL 1

Author Comment

by:rodneygray
ID: 34929990
Theres no pricing information but this looks like what I need. Will put it to use and get back to you, thanks.
0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34930086
Yes, I was trying to give you an idea of duplicating the preg_match pattern from your PHP code to display more info about the item.

For example you might want to have other options like:
<item-discount>20%</item-discount>
<item-detail>This item will be released next month</item-detail>

Regards...
0
 
LVL 1

Author Comment

by:rodneygray
ID: 34930157
Not working, all items come out with N/A in the item name and image.
This is being done in Expression Web 4 and I'm using templates for the client. Is the editable region information going to interfere with this?
0
 
LVL 1

Author Comment

by:rodneygray
ID: 34930164
Here is an example.
<h1><!--<item-name>--><!-- #BeginEditable "producttitle" -->NASCAR Deluxe Baby 
				  Crib<!-- #EndEditable --><!--</item-name>--></h1>

Open in new window

0
 
LVL 5

Accepted Solution

by:
onemadeye earned 250 total points
ID: 34930282
My modified PHP code will only working with my modified HTML code with the given rules :(

Well, assuming that :
- Item name will displayed inside <item-name>
- Item price will displayed inside <item-price>
- Item image will displayed inside <item-image>

Try use this PHP code :
<?php
	$files = glob("*.html");
	$ignore_files = array( //add any file names of files you don't want to appear in the site map
		"formsubmit.php",
		"thankyou.html",
		"_vti_inf.html",
		"postinfo.html",
	);

	foreach($files as $f){ 
		if(in_array($f,$ignore_files)) continue;
		$file_data = file_get_contents($f);

		if(preg_match('/<title>([^<]+)<\/title>/i',$file_data,$matches)){
		$page_title = $matches[1];
		}else{
		$page_title = $f;
		}

		if(preg_match('/<!--<item-name>-->*(.*?)\<!--<\/item-name>-->/i',$file_data,$matches)){
		$item_name = $matches[1];
		}else{
		$item_name = 'N/A';
		}

		if(preg_match('/<!--<item-price>-->*(.*?)\<!--<\/item-price>-->/i',$file_data,$matches)){
		$item_price = $matches[1];
		}else{
		$item_price = 'N/A';
		}

		// different preg_match pattern used
		if(preg_match('/<!--<item-image>-->*(.*?)\<!--<\/item-image>-->/i',$file_data,$matches)){
		$item_image = $matches[1];
		}else{
		$item_image = 'N/A';
		}
		
?>
<li>
<a href="<?=$f;?>"><?=$page_title;?></a><br />
Item Name: <?=$item_name?><br />
Item Price: <?=$item_price?><br />
Item Image: <?=$item_image?><br />
</li>
<?php
	} // END foreach
?>

Open in new window

0
 
LVL 1

Author Comment

by:rodneygray
ID: 34942520
Here is the page code.
Is there anyway to get this to work?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Products</title>
<link href="../../css/styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../js/jquery.js"></script>
<!--below is the code to display the website properly in ie6-->
<!--[if IE 6]>
<script defer type="text/javascript" src="../../js/pngfix.js"></script>
<![endif]-->
<!--[if IE]>
<style type="text/css" media="all">

@font-face {
 font-family: "Emma Sophia";
 src: url(font/emmasophia.eot) !important;
}
.auto-style1 {
	color: #00B2D9;
}
</style>
<![endif]-->
</head>
<body>
<div id="background">
  <!-- tiling image of the blue gradient at the top -->
  <div id="centre">
    <!-- splats and stars -->
    <div id="water_sand">
      <!-- the tiling water and sand wave along the bottom -->
      <div id="bottom">
        <!--this is the underwater scene -->
        <div id="wrapper">
          <div id="header">
            <div id="logo_birds">
              <div id="logo_container">
                <div id="logo">
					<img src="../../images/logo.png" width="262" height="64" alt="Logo" /></div>
              </div>
              <div class="left"></div>
              <div class="right"></div>
            </div>
            <!--end logo birds-->
            <div id="search_blimp">
              <div class="front">
				  <img src="../../images/search_blimp.png" width="213" height="63" alt="Search" /></div>
              <div class="middle">
                <div class="field">
                  &nbsp;Over 10 Years in Business!</div>
              </div>
              <div class="tail"></div>
            </div>
            <!--end search blimp-->
          </div>
          <!--end header-->
          <div id="menu_wrapper">
            <div id="main_menu">
              <ul>
                <li><a href="../../index.html">Home</a></li>
                <li><a href="../../products.html">Products</a></li>
                <li class="auto-style1"><a href="../../product_full.html">Gift&nbsp;Registry</a></li>
                <li> <a href="../../about.html">about</a></li>
                <li><a href="../../terms_faq.html">terms &amp; faq</a></li>
                <li class="noborder"><a href="../../contact.html">contact</a></li>
              </ul>
            </div>
            <!--end main menu-->
            <div id="cart">
              <div class="left"></div>
				<h3 class="h3phone">Phone: 912-260-1268</h3>
              <div class="right"></div>
            </div>
            <!--end cart-->
          </div>
          <!--end menu wrapper-->
          <div id="container">
            <div class="top"></div>
            <div id="content">
              <!--start site map code-->
              <h1>Products</h1>
              <ul>
			  <?php
				$files = glob("*.html");
				$ignore_files = array( //add any file names of files you don't want to appear in the site map
					"formsubmit.php",
					"thankyou.html",
					"_vti_inf.html",
					"postinfo.html",
				);
			
				foreach($files as $f){ 
					if(in_array($f,$ignore_files)) continue;
					$file_data = file_get_contents($f);
			
					if(preg_match('/<title>([^<]+)<\/title>/i',$file_data,$matches)){
					$page_title = $matches[1];
					}else{
					$page_title = $f;
					}
			
					if(preg_match('/<item-name>([^<]+)<\/item-name>/i',$file_data,$matches)){
					$item_name = $matches[1];
					}else{
					$item_name = 'N/A';
					}
			
					// different preg_match pattern used
					if(preg_match('/<item-image>*(.*?)\<\/item-image>/i',$file_data,$matches)){
					$item_image = $matches[1];
					}else{
					$item_image = 'N/A';
					}
					?>
					
					<li>
						<a href="<?=$f;?>"><?=$page_title;?><br />
						Item: <?=$item_name?><br />
						Image: <?=$item_image?></a><br /> <br />
					</li>
				
				<?php
						} // END foreach
				?>
              
              </ul>
              <!--end site map code-->
              <hr class="clear" />
            </div></div></div></div>
            <!--end content-->
          </div>
          <!--end main container-->
          <div id="container_bottom"></div>
          <div id="footer_container">
            <div id="footer">
              <ul>
                <li>Kibs and Kradles |  &copy; 2011</li>
                <li><a href="sitemap.php">Site Map</a> |</li>
                <li><a href="contact.html">Contact Us</a></li>
              </ul>
            </div>
        <!--end wrapper-->
      </div>
      <!--end bg pattern-->
  </div>
</div>
</body>
</html>

Open in new window

0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34946398
Okay, here's your page code modified.
And if this still not works, try to paste the source code from one of your HTML file (perhaps via View Source when accessed from browser).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Products</title>
<link href="../../css/styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../js/jquery.js"></script>
<!--below is the code to display the website properly in ie6-->
<!--[if IE 6]>
<script defer type="text/javascript" src="../../js/pngfix.js"></script>
<![endif]-->
<!--[if IE]>
<style type="text/css" media="all">

@font-face {
 font-family: "Emma Sophia";
 src: url(font/emmasophia.eot) !important;
}
.auto-style1 {
	color: #00B2D9;
}
</style>
<![endif]-->
</head>
<body>
<div id="background">
  <!-- tiling image of the blue gradient at the top -->
  <div id="centre">
    <!-- splats and stars -->
    <div id="water_sand">
      <!-- the tiling water and sand wave along the bottom -->
      <div id="bottom">
        <!--this is the underwater scene -->
        <div id="wrapper">
          <div id="header">
            <div id="logo_birds">
              <div id="logo_container">
                <div id="logo">
					<img src="../../images/logo.png" width="262" height="64" alt="Logo" /></div>
              </div>
              <div class="left"></div>
              <div class="right"></div>
            </div>
            <!--end logo birds-->
            <div id="search_blimp">
              <div class="front">
				  <img src="../../images/search_blimp.png" width="213" height="63" alt="Search" /></div>
              <div class="middle">
                <div class="field">
                  &nbsp;Over 10 Years in Business!</div>
              </div>
              <div class="tail"></div>
            </div>
            <!--end search blimp-->
          </div>
          <!--end header-->
          <div id="menu_wrapper">
            <div id="main_menu">
              <ul>
                <li><a href="../../index.html">Home</a></li>
                <li><a href="../../products.html">Products</a></li>
                <li class="auto-style1"><a href="../../product_full.html">Gift&nbsp;Registry</a></li>
                <li> <a href="../../about.html">about</a></li>
                <li><a href="../../terms_faq.html">terms &amp; faq</a></li>
                <li class="noborder"><a href="../../contact.html">contact</a></li>
              </ul>
            </div>
            <!--end main menu-->
            <div id="cart">
              <div class="left"></div>
				<h3 class="h3phone">Phone: 912-260-1268</h3>
              <div class="right"></div>
            </div>
            <!--end cart-->
          </div>
          <!--end menu wrapper-->
          <div id="container">
            <div class="top"></div>
            <div id="content">
              <!--start site map code-->
              <h1>Products</h1>
              <ul>
			  <?php
				$files = glob("*.html");
				$ignore_files = array( //add any file names of files you don't want to appear in the site map
					"formsubmit.php",
					"thankyou.html",
					"_vti_inf.html",
					"postinfo.html",
				);
			
				foreach($files as $f){ 
					if(in_array($f,$ignore_files)) continue;
					$file_data = file_get_contents($f);
			
		if(preg_match('/<title>([^<]+)<\/title>/i',$file_data,$matches)){
		$page_title = $matches[1];
		}else{
		$page_title = $f;
		}

		if(preg_match('/<!--<item-name>-->*(.*?)\<!--<\/item-name>-->/i',$file_data,$matches)){
		$item_name = $matches[1];
		}else{
		$item_name = 'N/A';
		}

		// different preg_match pattern used
		if(preg_match('/<!--<item-image>-->*(.*?)\<!--<\/item-image>-->/i',$file_data,$matches)){
		$item_image = $matches[1];
		}else{
		$item_image = 'N/A';
		}

		if(preg_match('/<!--<item-price>-->*(.*?)\<!--<\/item-price>-->/i',$file_data,$matches)){
		$item_price = $matches[1];
		}else{
		$item_price = 'N/A';
		}
					?>
					<li>
						<a href="<?=$f;?>"><?=$page_title;?><br />
						Item: <?=$item_name?><br />
						Image: <?=$item_image?></a><br /><br />
					</li>				
				<?php
						} // END foreach
				?>
              </ul>
              <!--end site map code-->
              <hr class="clear" />
            </div></div></div></div>
            <!--end content-->
          </div>
          <!--end main container-->
          <div id="container_bottom"></div>
          <div id="footer_container">
            <div id="footer">
              <ul>
                <li>Kibs and Kradles |  &copy; 2011</li>
                <li><a href="sitemap.php">Site Map</a> |</li>
                <li><a href="contact.html">Contact Us</a></li>
              </ul>
            </div>
        <!--end wrapper-->
      </div>
      <!--end bg pattern-->
  </div>
</div>
</body>
</html>

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php56-php-mcrypt for rhel7 php56 1 56
multiple selects 23 49
Do alert on select 6 15
JavaScript Loan Calculator Error 3 19
This article discusses how to create an extensible mechanism for linked drop downs.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

820 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