Solved

I need help with a PHP page collector

Posted on 2011-02-18
11
278 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now