Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I need help with a PHP page collector

Posted on 2011-02-18
11
Medium Priority
?
285 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1000 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

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!

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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