PDA

View Full Version : Want to Display output from a foreach in a template


Gizmo99
11-14-2009, 04:58 PM
Ok

Simple thing I have a Form that people can put membership numbers into, then a script will check in Mysql if they are or not

Then print out Yes or No and expiry if Yes

Works fine for single lookup but make it do more lookup ie 8 the output templete just shows last lookup in the list result.

if you just print the out ot the screen all the 8 results are there

Help


foreach ( $_POST as $name => $value) {
if ((stristr($name,'membership')) && ($value !=0 ) ) {
// Check to See if Member
$check_if_member=$db->query_first("SELECT MemberID, DateExpire FROM ~~~~~ WHERE MemberID = $value");

if ($check_if_member){
$membership_number=$value;
$Expire_Date=$check_if_member['DateExpire'];
}
else
{
$membership_number=$value;
$Expire_Date="" ;
}
print_r ($check_if_member) ;
echo "<br /> $name, $value <br />" ;
}

eval('print_output("'. fetch_template('rsoc_member_check_output') . '");');


Screen output

membership_number2, 1

membership_number3, 2

membership_number4, 3

membership_number5, 2

membership_number6, 34
Array ( [MemberID] => 14**** [DateExpire] => *******)
membership_number8, 14****


What do i need to either do in the script or template to output the above ?

* = protect data

Giz

kh99
11-15-2009, 01:07 PM
I think what I'd do is make the entire list one variable in the main template, then create another small template to display one membership line. Then construct that list variable in the loop so it will be ready to use when you call "fetch_template" for the main template at the end.

Gizmo99
11-15-2009, 06:03 PM
Thanks for the reply mate, can u give some demo code of your suggestion

Tar muchly

Gizmo

kh99
11-15-2009, 06:43 PM
Something like this (I didn't actually try this so you'll have to excuse me if there's some small error:

$memlist = '';

foreach ( $_POST as $name => $value) {
if ((stristr($name,'membership')) && ($value !=0 ) ) {
// Check to See if Member
$check_if_member=$db->query_first("SELECT MemberID, DateExpire FROM ~~~~~ WHERE MemberID = $value");

if ($check_if_member){
$membership_number=$value;
$Expire_Date=$check_if_member['DateExpire'];
}
else
{
$membership_number=$value;
$Expire_Date="" ;
}
print_r ($check_if_member) ;
//echo "<br /> $name, $value <br />" ;
eval('$memlist .= "' . fetch_template('membership_template') . '";');
}

eval('print_output("'. fetch_template('rsoc_member_check_output') . '");');

I added the parts in red. The small template (which I called 'membership_template' but you can of course call whatever you want) can probably be created by taking the corresponding part out of the 'rsoc_member_check_output' template.

In fact, if you wanted you could just use
$memlist .= "<br /> $membership_number, $Expire_Date<br />";
(or whatever HTML you want) in place of the 'eval' line and not use a template.

In any case, in your 'rsoc_member_check_output' template just put $memlist where you want the list.

--------------- Added 1258324902 at 1258324902 ---------------

Sorry, I realized that you want to use $membership_number and $Expire_Date instead of $name and $value (which I guess were in the echo call for debugging?) so I changed the above.

Gizmo99
11-15-2009, 08:44 PM
Sorted Mate was nearly there but u helped with the last bit

Cheers again Mate

// Set Up the Array
$member_output = '';

// Go through all the Membership Number Entered
foreach ( $_POST as $name => $value) {

// Check we only check filled out fileds
if ((stristr($name,'membership')) && ($value !=0 ) ) {

// Check to See if Member
$check_if_member=$db->query_first("SELECT MemberID, DateExpire FROM ####### WHERE MemberID = $value");

// Set up the Arrays Elements based on Membership Detials
if ($check_if_member){
$membership_result[member]=$value;
$membership_result[expire]=$check_if_member['DateExpire'];

}
else
{

// Not A Member Set Up
$membership_result[member]=$value;
$membership_result[expire]="NOT A MEMBER";

}

// Build Array for Template
$member_output .="<br /> Membership Number : $membership_result[member]<br />";
$member_output .="Member or Expiry : $membership_result[expire] <br />";

}
}

// Print the Answer to the search
eval('print_output("'. fetch_template('rsoc_member_check_output') . '");');

}

?>