Link to home
Start Free TrialLog in
Avatar of rodneygray
rodneygrayFlag for United States of America

asked on

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

Avatar of AriMc
AriMc
Flag of Finland image

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?

Avatar of rodneygray

ASKER

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of onemadeye
onemadeye
Flag of Indonesia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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