Jump to content

getting a part of the content of a page


Alkimuz

Recommended Posts

my end goal is to show an invoice with data of the customer, therefor i made a templatefile with pure html and within a function, i get the content of the templatefile and replace specific words in the templatefile with the data of the customer. so far so good.

 

but now, i have a repeatitive part of the templatefile, the one with the bought items.. my idea is to substract that specific part out of the variable with the content of the templatefile, duplicate and fill it with a while loop and put it back in the variable, replacing the original.. (hopefully your still with me?)

 

this works:

$url = $_SESSION['dir'].'php/invoice.php';
  $invoice = file_get_contents($url);

//replace data 
	$replacement = array("name", "prefix", "ordernumber", "orderdate", "address", "zipcode", "place", "country", "ordername", "orderaddress", "orderzipcode", "orderplace", "ordercountry");	
	foreach ($replacement as $word){
	  $invoice = str_replace('{$'.$word.'}', $data[$word], $invoice);
	}

so the whole invoice code is in $invoice and the data of the customer is allready in it

 

now time for the next step: the substraction of the specific part, which is between these tags: <!-- repeat --> <!-- /repeat -->

 

i was thinking of:

preg_match("/<!-- repeat -->(.*)<!-- \/repeat -->/", $invoice, $invoicepart);

but that does not work.. :-\ is that because the syntax is wrong or because the part to select excists of a lot of html code?

 

after that, i was think of:

 

$itemresult = item_query();			
while ($itemrow = mysql_fetch_array($itemresult)){

        //replace item data 
	$temporalpart = $invoicepart;
        $replacement = array("itemname", "itemamount", "itemprice", "itempricetotal");	
	foreach ($replacement as $word){
	      $temporalpart= str_replace('{$'.$word.'}', $itemrow[$word], $temporalpart);
	}
       $newinvoicepart.= $temporalpart
}

$invoice = str_replace("$invoicepart", "$newinvoicepart", "$invoice");

would that be the right way to go further?


many thanks for helping me!
 

 

 

 

Link to comment
Share on other sites

The regex won't work because you're using special characters as literals. This is purely from memory but you'll need to escape < and ! characters.

 

Also, str_replace will accept arrays as parameters. So as long as you make sure they're in the correct order:

 

$find = array( 'foo', 'bar' );

$replacements = array( 'Yo', 'Hoe');

$str = 'foo bar';

echo str_replace( $find, $replacements, $str );

//this is wrong btw
str_replace('{$'.$word.'}', $itemrow[$word], $temporalpart);

//needs to be this if you're going down that route
str_replace($word, $itemrow[$word], $temporalpart);

 

Hope that helps :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.