I need help with a PHP page collector

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

LVL 1
rodneygrayAsked:
Who is Participating?
 
onemadeyeCommented:
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
 
AriMcCommented:
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
 
rodneygrayAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
rodneygrayAuthor Commented:
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
 
onemadeyeCommented:
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
 
rodneygrayAuthor Commented:
Theres no pricing information but this looks like what I need. Will put it to use and get back to you, thanks.
0
 
onemadeyeCommented:
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
 
rodneygrayAuthor Commented:
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
 
rodneygrayAuthor Commented:
Here is an example.
<h1><!--<item-name>--><!-- #BeginEditable "producttitle" -->NASCAR Deluxe Baby 
				  Crib<!-- #EndEditable --><!--</item-name>--></h1>

Open in new window

0
 
rodneygrayAuthor Commented:
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
 
onemadeyeCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.