Jump to content

while loop not working


scarface83

Recommended Posts

Hi in the db there is more than one record but my code is only showing one line can anyone see where im going wrong ?

<?php
	session_start();


	include ('db_connect.inc');

	$query = " SELECT TeamRef FROM a_users WHERE Login='$keepit'  ";
	$result = mysql_query($query) or die (mysql_error); // runs the above query
	$row = mysql_fetch_array($result);
	$tm = $row['TeamRef'] ;

	$query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; 

	$result2 = mysql_query($query2) or die (mysql_error); // runs the above query
	$row2 = mysql_fetch_assoc($result2);
	$date_stamp =$row2['datestamp'] ;

	$new_id	=	$row2['ID'];

	list($year,$month,$day) = split("-",$date_stamp);
	$newdate = $day."/".$month."/".$year;


?>

<!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=iso-8859-1" />
<title>Untitled Document</title>
<link href="/ab_mgt/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="title"> <u><b>Pending Absence for your Team</b></u> </div>
<div class="pend_subtitle"> Please Confirm if this absence should be saved to your absence calendar ? </div>
<div class="back"><a href="main.php">Back</a></div>

<div class="pend_absence">

<?php 
	$query3 = mysql_query("SELECT Forename,Surname FROM a_users WHERE In_use='Y' AND Login='$new_id'")or die (mysql_error);

	$row3 = mysql_fetch_assoc($query3);
	session_unregister('first');
	session_unregister('second');
	$first = $row3['Forename'];
	$second = $row3['Surname'];



	while ($row2 = mysql_fetch_assoc($result2));{
	echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second."   <a href= 'test' >Save</a>";

}
?>	

Link to comment
Share on other sites

<?php
	session_start();


	include ('db_connect.inc');

	$query = " SELECT TeamRef FROM a_users WHERE Login='$keepit'  ";
	$result = mysql_query($query) or die (mysql_error); // runs the above query
	$row = mysql_fetch_array($result);
	$tm = $row['TeamRef'] ;

	$query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; 

	$result2 = mysql_query($query2) or die (mysql_error); // runs the above query
	$row2 = mysql_fetch_assoc($result2);
	$date_stamp =$row2['datestamp'] ;

	$new_id	=	$row2['ID'];

	list($year,$month,$day) = split("-",$date_stamp);
	$newdate = $day."/".$month."/".$year;


?>

<!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=iso-8859-1" />
<title>Untitled Document</title>
<link href="/ab_mgt/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="title"> <u><b>Pending Absence for your Team</b></u> </div>
<div class="pend_subtitle"> Please Confirm if this absence should be saved to your absence calendar ? </div>
<div class="back"><a href="main.php">Back</a></div>

<div class="pend_absence">

<?php 
	$query3 = mysql_query("SELECT Forename,Surname FROM a_users WHERE In_use='Y' AND Login='$new_id'")or die (mysql_error);

	$row3 = mysql_fetch_assoc($query3);
	session_unregister('first');
	session_unregister('second');
	$first = $row3['Forename'];
	$second = $row3['Surname'];



	while ($row2 = mysql_fetch_assoc($result2))
{
	$first = $row2['Forename'];
	$second = $row2['Surname'];
	$second = $row2['Surname'];
	$new_id= $row2['new_id'];
	$newdate= $row2['newdate'];

	echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second."   <a href= 'test' >Save</a>";

}
?>	

Link to comment
Share on other sites

change

 

while ($row2 = mysql_fetch_assoc($result2))

 

to

 

while ($row2 == mysql_fetch_assoc($result3))

 

your need to update

$query3 = mysql_query("SELECT Forename,Surname FROM a_users WHERE In_use='Y' AND Login='$new_id'")or die (mysql_error);

 

with the extra fields

Link to comment
Share on other sites

<?php
	session_start();


	include ('db_connect.inc');

	$query = " SELECT TeamRef FROM a_users WHERE Login='$keepit'  ";
	$result = mysql_query($query) or die (mysql_error); // runs the above query
	$row = mysql_fetch_array($result);
	$tm = $row['TeamRef'] ;

	$query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; 

	$result2 = mysql_query($query2) or die (mysql_error); // runs the above query
	$row2 = mysql_fetch_assoc($result2);
	$date_stamp =$row2['datestamp'] ;

	$new_id	=	$row2['ID'];

	list($year,$month,$day) = split("-",$date_stamp);
	$newdate = $day."/".$month."/".$year;


?>

<!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=iso-8859-1" />
<title>Untitled Document</title>
<link href="/ab_mgt/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="title"> <u><b>Pending Absence for your Team</b></u> </div>
<div class="pend_subtitle"> Please Confirm if this absence should be saved to your absence calendar ? </div>
<div class="back"><a href="main.php">Back</a></div>

<div class="pend_absence">

<?php 
	$query3 = mysql_query("SELECT Forename,Surname FROM a_users WHERE In_use='Y' AND Login='$new_id'")or die (mysql_error);

	$row3 = mysql_fetch_assoc($query3);
	session_unregister('first');
	session_unregister('second');
	$first = $row3['Forename'];
	$second = $row3['Surname'];



	while ($row2 = mysql_fetch_assoc($result2)){
	echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second."   <a href= 'test' >Save</a>";

}
?>		
</div>

<?php mysql_close();
?>
</body>
</html>

Link to comment
Share on other sites

theirs more than one problem with the code

 

this is what you should have

 

After looking over the code as a whole.. one question remains.. what are you trying to do ?

a lot of the code isn't being used or is useless!

 

<?php
session_start();


include ('db_connect.inc');

$query = " SELECT TeamRef FROM a_users WHERE Login='$keepit' ";//WILL FAIL as $keepit isn't set
$result = mysql_query($query) or die (mysql_error()); // runs the above query
$row = mysql_fetch_array($result);
$tm = $row['TeamRef'];

$query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; 

$result2 = mysql_query($query2) or die (mysql_error()); // runs the above query
$row2 = mysql_fetch_assoc($result2);				

?>

<!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=iso-8859-1" />
<title>Untitled Document</title>
<link href="/ab_mgt/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="title"> <u><b>Pending Absence for your Team</b></u> </div>
<div class="pend_subtitle"> Please Confirm if this absence should be saved to your absence calendar ? </div>
<div class="back"><a href="main.php">Back</a></div>

<div class="pend_absence">

<?php 
$query3 = mysql_query("SELECT datestamp, ID, Forename,Surname FROM a_users ")or die (mysql_error);
//
//Also remove the following while testing - WHERE In_use='Y' AND Login='$new_id'

$row3 = mysql_fetch_assoc($query3);
session_unregister('first');
session_unregister('second');

while ($row3 = mysql_fetch_assoc($result3))
{
	$first = $row3['Forename'];
	$second = $row3['Surname'];
	$date_stamp =$row2['datestamp'];		
	$new_id	= $row2['ID'];
	list($year,$month,$day) = split("-",$date_stamp);
	$newdate = $day."/".$month."/".$year;

	echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second."   <a href= 'test' >Save</a>";
}


mysql_close();
?>
</div>
</body>
</html>

Link to comment
Share on other sites

absence _mgt the a_users just grabs some values to add to the criteria.

 

Field Type    Null          Default

datestamp    date            Yes   

abs_value      smallint(1)    Yes   

ID              varchar(10)    Yes   

notes          varchar(100)    Yes   

viewable    smallint(1)        Yes   

TeamRef    smallint(2)        Yes   

 

 

Link to comment
Share on other sites

erm..

i'm confused

you say that this works "SELECT TeamRef FROM a_users WHERE Login='$keepit'"

which is the a_users, which is just for the TeamRef, ok so far makes sence,

 

BUT

 

absence _mgt the a_users just grabs some values to add to the criteria.

Field Type    Null          Default

datestamp    date            Yes   

abs_value      smallint(1)    Yes   

ID              varchar(10)    Yes   

notes          varchar(100)    Yes   

viewable    smallint(1)        Yes   

TeamRef    smallint(2)        Yes   

 

Theirs no Forename or Surname in their so it must be in the a_users, so whats absence _mgt for ?

Link to comment
Share on other sites

he he sorry getting a little confused now so lets start again lol

 

this code grabs the team reference of the users where login = $keepit - which is there login, i have etested this and it works if you echo it out after the query

<?php
session_start();


include ('db_connect.inc');

$query = " SELECT TeamRef FROM a_users WHERE Login='$keepit' ";//this is a session variable already set on another page
$result = mysql_query($query) or die (mysql_error()); // runs the above query
$row = mysql_fetch_array($result);
$tm = $row['TeamRef'];

this is the code that it important it grabs the datestamp and id where the team ref is the same as the user who login in

 

	$query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; // this query select the datestamp and users ID 

$result2 = mysql_query($query2) or die (mysql_error()); // runs the above query
$row2 = mysql_fetch_assoc($result2);				

?>

 

see comments in the code but this next part should be a loop and show all entries where the teamref = is = to that of the person who is logged in , when i view the page i get one result which is correct but there are about 12 entries so it seems the while loop isn't working correclty

 

<?php 
$query3 = mysql_query("SELECT datestamp, ID, Forename,Surname FROM a_users ")or die (mysql_error); // this can be ingnores as it works fine
//
//Also remove the following while testing - WHERE In_use='Y' AND Login='$new_id'

$row3 = mysql_fetch_assoc($query3);
session_unregister('first');
session_unregister('second');

while ($row3 = mysql_fetch_assoc($result3)) // the problem lies as it displays one row thats correct but doesnt show the others 
{
	$first = $row3['Forename']; 
	$second = $row3['Surname'];
	$date_stamp =$row2['datestamp'];		
	$new_id	= $row2['ID'];
	list($year,$month,$day) = split("-",$date_stamp);
	$newdate = $day."/".$month."/".$year;

	echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second."   <a href= 'test' >Save</a>";
}


mysql_close();

 

thanks for your help with this

 

Link to comment
Share on other sites


<?php 
$query3 = mysql_query("SELECT datestamp, ID, Forename,Surname FROM a_users ")or die (mysql_error); // this can be ingnores as it works fine
//
//Also remove the following while testing - WHERE In_use='Y' AND Login='$new_id'

$row3 = mysql_fetch_assoc($query3);// don't need this here
session_unregister('first');
session_unregister('second');

while ($row3 = mysql_fetch_assoc($result3)) // the problem lies as it displays one row thats correct but doesnt show the others 
{
	$first = $row3['Forename']; 
	$second = $row3['Surname'];
	$date_stamp =$row2['datestamp']; //<--Should be row3
	$new_id	= $row2['ID']; //<--Should be row3
	list($year,$month,$day) = split("-",$date_stamp);
	$newdate = $day."/".$month."/".$year;

	echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second."   <a href= 'test' >Save</a>";
}


mysql_close();

Link to comment
Share on other sites

these

 

$date_stamp =$row2['datestamp']; //<--Should be row3
$new_id	= $row2['ID']; //<--Should be row3

corresponds to this statement

 

$query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; // this query select the datestamp and users ID 

$result2 = mysql_query($query2) or die (mysql_error()); // runs the above query
$row2 = mysql_fetch_assoc($result2);				

so should be row2 ?

 

thanks

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.