Forum Moderators: coopster

Message Too Old, No Replies

include file within while loop

only includes file once for some reason

         

derek mcgilvray

9:25 am on Aug 19, 2010 (gmt 0)

10+ Year Member



Hi,

I am trying to include file2.php within a loop in file1.php.

The problem is it only works once, then the script stops. I think the problem lies within file2.php, because when I include testfile3.php within the same loop, it works fine.






    Here is part of file1.php:

while($i<$num){
$users_id=mysql_result($result,$i,"members.MEMBER");
$Name=mysql_result($result,$i,"members.Name");
$adno=mysql_result($result,$i,"members.adno");
$comment=mysql_result($result,$i,reporting_prep.comment");
//explode the Name
$fullName = explode(".", $Name);
$firstname = $fullName[0];
$surname = $fullName[1];

echo"<tr><td>$firstname $surname</td><td> <input type=\"hidden\" name=\"adno".$i."\" value=\"$adno\" /><input type=\"hidden\" name=\"num\" value=\"$num\" /><textarea rows=\"5\" cols=\"100\" name=\"comment".$i."\">$comment</textarea>";


include"file2.php";


$i++;
}








    And here is file2.php:


<?php
$userid="test";

if(isset ($_POST['submit0'])){
$focus=$_POST['focus'];

$query0="DELETE FROM assessment_focus_lookup WHERE userid='$userid'";
mysql_query($query0);

for ($j=0; $j<count($_POST['focus']); $j++){
$focus = addslashes($_POST['focus'][$j]);

$query00="INSERT INTO assessment_focus_lookup (userid, af_id) VALUES ('$userid', '$focus')";
mysql_query($query00) or die (mysql_error());
}//end for loop
}

//get any data from db
$query000="SELECT * FROM assessment_focus_lookup WHERE userid='$userid'";
$result000=mysql_query($query000) or die (mysql_error());

while ($row = mysql_fetch_assoc($result000)) {
$checked[] = $row['af_id'];
}

/* get the checkbox labels */
$focus = get_checkbox_labels("assessment_focus_reading");

/* create the html code for a formatted set of
checkboxes */
$html_focus = make_checkbox_html($focus, 7, 800, "focus[]");

global $focus;

$table_name="assessment_focus_reading";


?>


<html>
<body>
<br>
<form name="focus" method="POST" action="<?php echo $PHP_SELF;?>">

<? echo "$html_focus"; ?>
<br>
<input type="submit" name="submit0" value="Submit">
</form>
</body>
</html>
<?php

function get_checkbox_labels($table_name) {

/* make an array */
$arr = array();

/* construct the query */
$query0 = "SELECT * FROM $table_name ORDER BY id DESC";

/* execute the query */
$qid = mysql_query($query0);

/* each row in the result set will be packaged as
an object and put in an array */
while($row= mysql_fetch_object($qid)) {
array_push($arr, $row);
}
return $arr;
}

/* Prints a nicely formatted table of checkbox choices.

$arr is an array of objects that contain the choices
$num0 is the number of elements wide we display in the table
$width is the value of the width parameter to the table tag
$name is the name of the checkbox array
$checked is an array of element names that should be checked
*/

function make_checkbox_html($arr, $num0, $width, $name, $checked) {

global $checked, $userid, $name, $width;


/* create string to hold out html */
$str = "";

/* make it */
$str .= "<table width=\"$width\" border=\"1\">\n";
$str .= "<tr valign=\"top\">\n";

/* determine if we will have to close add
a closing tr tag at the end of our table */
if (count($arr) % $num0 != 0) {
$closingTR = true;
}

$j = 1;
if (ISSET($checked)) {

/* if we passed in an array of the checkboxes we want
to be displayed as checked */
foreach ($arr as $ele) {

$str .= "<td width=\"14%\">";
$str .= "<b>$ele->name</b><br />";
$str .= "$ele->af
<input type=\"hidden\" name=\"userid\" value=\"$userid\">
<input type=\"checkbox\" name=\"$name\" value=\"$ele->name\"";
foreach ($checked as $key=>$value) {
if ($value == $ele->name) {
$str .= "checked=\"checked\"";
continue;
}
}
$str .= ">";

if ($j % $num0 == 0) {
$str .= "</tr>\n<tr valign=\"top\">";
} else {
$str .= "</td>\n";
}
$j++;
}
} else {

/* we just want to print the checkboxes. none will have checks */
foreach ($arr as $ele) {

$str .= "<td width=\"14%\">";
$str .= "<b>$ele->name</b><br />";
$str .= "$ele->af
<input type=\"hidden\" name=\"userid\" value=\"$userid\">
<input type=\"checkbox\" name=\"$name\" value=\"$ele->name\">";

if ($j % $num0 == 0) {
$str .= "</tr>\n<tr valign=\"top\">";
} else {
$str .= "</td>\n";
}
$j++;
}
}

/* tack on a closing tr tag if necessary */
if ($closingTR == true) {
$str .= "</tr></table>\n";
} else {
$str .= "</table>\n";
}
return $str;
}
?>





If anyone has any ideas what I might be doing wrong, I'd appreciate any comments.

Thanks

morehawes

10:41 am on Aug 19, 2010 (gmt 0)

10+ Year Member



Hi, I haven't got time to have a proper look at your problem but perhaps consider re-thinking how you are going about this. If you are repeating the same logic contained in file2.php consider shifting this to a function instead of using an include. Also within file2.php you appear to have the full HTML tags for a page <html><head>...</head><body>...</body></html> which means you will have multiple sets embedded within one page. This won't make any sense to the browsers.

Matthew1980

11:11 am on Aug 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Not only that ^^, but your mixing tags for php - which is bad practise, your basically confusing the parser. Try to stick to: <?php ?> all the way through your project, this will negate headaches in the future should you wish to migrate servers/hosts as different servers mean different ini files, and as the tag preference is set in the ini file...

Cheers,
MRb



I seem to be saying this a lot recently - I hope that the manual states using full tags in the first sentace :)

This can potentially stop the script from functioning - Honestly! Try to avoid code dumps to, puts a lot of people off from answering your questions

derek mcgilvray

7:36 pm on Aug 19, 2010 (gmt 0)

10+ Year Member



Thanks for your time answering my queries on this.

I've corrected the tags and have thought about putting it into a function but have decided against this as I don't yet have the know-how.

The problem comes with how the functions are dealt with after the first loop. Nothing, not even a test string, is echoed after either of the functions in the second loop.

Any ideas?