Jump to content

looping help/confusion


fareforce

Recommended Posts

I have two php files. I am trying to make a statement that will pull all of the information from test2.php into test one.

 

test.php - main file

test2.php - information file

 

My thought was to turn test2.php into an array, and then pull it back into test.php using the foreach command. However, I can't get that to work. So it might not be the correct approch.

 

Currently test.php contains:

<?php
include ('test2.php');
preg_match ('@^(?:prop_code)?([^/]+)@i', "test2.php", $propnum);
foreach ($propnum as $V) 
{ 
      echo $V.'<br><br>'; 
} 
?>

 

I am not sure why but test.php always returns back whatever I have in the $subject section of the preg_match sytax.

test2.php<br><br>test2.php<br><br>

 

and test2.php contains:

<?php
// This is the file that controls what properties display
// Property images go in /images/properties/
// Keep each image in "" (ie. $pic1a = "1a.jpg"

// Property #1
$prop_code1 = "GN110";
$sqft1 = "1520";
$txt1 = "Very nice property";
$pic1a = "";
$pic1b = "";
$pic1c = "";
$pic1d = "";
$pic1e = "";
$pic1f = "";
$pic1g = "";

// Property #2
$prop_code2 = "DBP-B";
$sqft2 = "2165";
$txt2 = "Nice Midtown Property";
$pic2a = "";
$pic2b = "";
$pic2c = "";
$pic2d = "";
$pic2e = "";
$pic2f = "";
$pic2g = "";


// Property #3
$prop_code3 = "ENLB246";
$sqft3 = "3250";
$txt3 = "Nice Space";
$pic3a = "";
$pic3b = "";
$pic3c = "";
$pic3d = "";
$pic3e = "";
$pic3f = "";
$pic3g = "";


// Property #4
$prop_code4 = "555-942";
$sqft4 = "1460";
$txt4 = "Very nice property";
$pic4a = "";
$pic4b = "";
$pic4c = "";
$pic4d = "";
$pic4e = "";
$pic4f = "";
$pic4g = "";

?>

 

test2.php will always be in the same format (although I can change the format if needed). My end result needs to basically display the following for each $prop_code? (? = whatever the number is):

$prop_code? <br />
$sqft? <br />
$txt? <br />
$pic?a <br />
$pic?b <br />
$pic?c <br />
$pic?d <br />
$pic?e <br />
$pic?f <br />
$pic?g <br />

 

Is an array the right way to do this, and is all of my formatting for test2.php correct.

Link to comment
https://forums.phpfreaks.com/topic/123464-looping-helpconfusion/
Share on other sites

I was playing around a bit, and came up with this:

 

<?php
include ('test2.php');
preg_match_all ("/prop_code[0-9] = (.*)/", $props, $val, PREG_SET_ORDER);
foreach ($val as $propcode) 
{ 
    echo "Property Code: " . $propcode[1] . "<br /><br />"; 
} 
?>

 

And it is pulling the right info, but it is also only pulling the prop_code field. How can I get it to pull the rest of the information that goes with each prop_code?

 

I also had to change test2.php to this format to get the above to work:

<?php
$props = "
// This is the file that controls what properties display
// Property images go in /images/properties/
// Keep each image in  (ie. pic1a = 1a.jpg)

// Property #1
prop_code1 = GN110
sqft1 = 1520
txt1 = Very nice property
pic1a = 
pic1b = 
pic1c = 
pic1d = 
pic1e = 
pic1f = 
pic1g = 

// Property #2
prop_code2 = DBP-B
sqft2 = 2165
txt2 = Nice Midtown Property
pic2a = 
pic2b = 
pic2c = 
pic2d = 
pic2e = 
pic2f = 
pic2g = 


// Property #3
prop_code3 = ENLB246
sqft3 = 3250
txt3 = Nice Space
pic3a = 
pic3b = 
pic3c = 
pic3d = 
pic3e = 
pic3f = 
pic3g = 


// Property #4
prop_code4 = 555-942
sqft4 = 1460
txt4 = Very nice property
pic4a = 
pic4b = 
pic4c = 
pic4d = 
pic4e = 
pic4f = 
pic4g = 

"
?>

ara you try this

test.php

<?php
$array = parse_ini_file('test2.php',1);
print_r($array);
?>

and test2.php

[prop_1]
prop_code = GN110
sqft = 1520
txt = Very nice property
pica = 
picb = 
picc = 
picd = 
pice = 
picf = 
picg = 
[prop_2]
prop_code = GN110
sqft = fdbg h  h n gg 
txt = Very nice property
pica = 
picb = 
picc = 
picd = 
pice = 
picf = 
picg = 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.