Jump to content

Nodral

Members
  • Posts

    397
  • Joined

  • Last visited

Posts posted by Nodral

  1. Hi

     

    I have a form that returns 5 variables from user submitted fields when the submit button is pressed.  I need my script to react in different ways depending on how many of the fields are completed.

     

    The form is made up of 20 rows with each line having a unique id number which prefixes each variable, for example

    $1forename, $1surname, $1address, $1phone, $1email

    $2forename, $2surname, $2address, $2phone, $2email

    .

    .

    .

    .

    .

    .

    .

    .

    $20forename, $20surname, $20address, $20phone, $20email

     

    The user can complete as many or as few lines as they require

     

    If no fields on a line are completed, i need it to ignore the script, if all fields on a line are completed write to a DB, and if the line on the form is partially completed return an error message.

     

    I can't use isset, as even if the user just clicks on submit then a null/blank value is returned and the variable is set for every field on every line

     

     

    Anyone any ideas how I can acheive this without using a seperate if(strlen($variable)>0){ declaration for every variable.

     

    Hope this makes sense and I look forward to some of you wonderful guys giving me a nice concise solution.

     

    Cheers

     

  2. When you create your while loop,  have it output as a form within the loopand include a hidden input field of the drivers id (da)  once the user clicks on this, run a seperate sql query to return the driver details.

     

    <?php
    
    //has driver been selected from form?
    if(isset($_POST['submit'])){
    // get details for selected driver
    $sql="SELECT * FROM driver  WHERE id=" . $_POST['id'];
    $sql=mysql_query($sql);
    $row=mysql_fetch_array($sql);
    
    //echo all details.................
    }
    
    
    //get info for all drivers
    $sql="SELECT * FROM driver";
    $sql=mysql_query($sql);
    
    //start form
    echo'<form method="POST" action="">';
    
    while($driver=mysql_fetch_array($sql)){
    
    //populate each line with hidden id field
    echo $driver['fname'] . " " . $driver['mname'] . " " . $driver['lname'] . '<input type="hidden" name="id" value="'$driver['id'] .'"><input type="submit" name="submit" value="Get Details">';
    }
    
    //end form
    </form> 

  3. Hi

     

    I have a form which requires 5 values prior to submission.  I need to run an isset to establish whether all the fields are complete, which I can do.  I also need to run an !isset to establish if none of the fields are complete, which again I can do.  However, how do I establish whether only some of the fields are complete to return a message that there is missing information from the form?

  4. $password comes from a form and I know it has value as I've echoed it out in the top of the code I posted, as a test.

     

    I didn't think you'd appreciate 200 lines of code being posted, so I just posted the relevant parts.

     

    It's not writing to the DB which indicates that the if(strlen() function has an error

  5. Hi

     

    I've written a bit of code to validate whether a user has typed into a password box by using

    if(strlen($password>0)){ // ok to save password. 
    }else{ //give error message
    }
    

    However this is constntly saying the password is blank.  Any ideas?

     

    My actual code follows below.  I know that the $password variable is set as I've echoed it out as a test.

     

    <?php if(isset($_POST['pconf'])){
    		//has a password been entered?
    		echo $password;
    			if(strlen($password >0)){
    				//do new passwords match?
    				if($pword===$password){
    					//encode pword
    					$pword=md5($pword);
    					//write to DB
    					$sql="UPDATE pfp_user SET firstname = '$firstname', password = '$password', lastname = '$lastname', email = '$email', dept = '$dept', workplace = '$workplace' , home = '$home', complete = 'yes'  WHERE id = " . $_SESSION['USERID'];
    					mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
    					//redirect back to menu page
    					$_SESSION['output']='Your user profile has been successfully updated';
    					$extra="menu.php";
    					header("Location: http://$host$uri/$extra");
    				}else{
    					$_SESSION['warning']="Please re-enter your passwords as they do not match";
    					$_POST['pass']="";
    				}
    			}else{
    				$_SESSION['warning']="You cannot have a blank password<br>Please re-enter.";
    				$_POST['pass']="";
    			}
    	}else{
    		$sql="UPDATE pfp_user SET firstname = '$firstname', lastname = '$lastname', email = '$email', dept = '$dept', workplace = '$workplace' , home = '$home', complete = 'yes'  WHERE id = " . $_SESSION['USERID'];
    		mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
    		//redirect back to menu page
    		$_SESSION['output']='Your user profile has been successfully updated';
    		$extra="menu.php";
    		header("Location: http://$host$uri/$extra");}?>

  6. I've been through and checked all my closing brackets and braces and these all seem correct, as do my semi-colons

     

    As I said, this is purely a last resort to find the error as I'm baffled.  If it makes any difference, this worked until my server was upgraded to PHP 5.3?

  7. Hi

     

    I've been through this code over and over but cannot find why I'm getting an unexpected T_ELSE on line 481.

     

    Any ideas?  Sorry about the length of code, but this is a last resort to find the error.

     

    <?php
    $today=getdate();
    $today=$today[0];
    $quizname="Fleet";
    $region=$_POST['region'];
    $User_region=$_POST['user_region'];
    $quizid=$_POST['quizid'];
    //$quizname=$_POST['quizname'];
    $UserID=$_POST['userid'];
    $admin=$_POST['admin'];
    $counter=$_POST['counter'];
    $admin_report=$_POST['admin_report'];
    $reset=$_POST['reset'];
    $course = 236;
    if(isset($reset)){
    unset($UserID, $region, $User_region, $admin, $admin_report, $reset);
    }
    
    error_reporting(E_ALL);
    //added extra brace to prevent report working whilst user auth is added
    require_once(dirname(__FILE__) . '/../config.php');
    require_once(dirname(__FILE__) . '/functions.php');
    //require_once(dirname(__FILE__) . '/index2.php');
    $USERID=$USER->id;
    
    
    
    
    
    
    ?>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
      <TITLE> POST Course Evaluation Results </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Stu Watson" CONTENT="">
      <META http-equiv="Content-Style-Type" content="text/css">
      <META NAME="Description" CONTENT="">
    
      <!--Styles for report-->
      <style>
      td {font-size:75%; text-align:center}
      .outer{border:2px solid black;border-collapse:collapse; padding:5px; }
      .inner{border:1px solid gray;border-collapse:collapse;}
      tr.base{background-color:yellow;border-top-style:hidden;border-collapse:collapse;}
      tr.internal{border-bottom-style:hidden;border-collapse:collapse;}
      .over{background-color:green; font-weight:bold;}
      .under{background-color:red; font-weight:bold;}
    .alone{border:2px solid black;border-collapse:collapse; padding:5px; background-color:yellow; }
    
      </style>
    </HEAD>
    <?php
    
    if($USERID !== 0) {
    //Check whether user is logged in and if they have the correct role assignment to report.
    
    if(user_has_role_assignment($USER->id, 1) || user_has_role_assignment($USER->id, 36)) {
    	if(user_has_role_assignment($USER->id, 1)){
    		echo'<form method="POST" action=""><input type="hidden" name="admin" value="admin"><input type="submit" value="Admin Info"></form>';
    		if(!table_exists("qreports")){
    			$sql="CREATE TABLE qreports (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, userid INT NOT NULL, username VARCHAR(255), date VARCHAR(255), quizid INT NOT NULL)";
    			mysql_query($sql) or die ('Invalid query: ' . mysql_error());
    		}
    	}else{
    
    		// set variable for admin report of users accessing site
    		$counter++;
    	}
    ?>
    <BODY>
    <p><h3><center>Post Course Evaluation Results</center></h3>
    
    <!-- Reset Button-->
    <form method="POST" action="">
    <input type="hidden" name="counter" value="<?php echo $counter; ?>">
    <input type="submit" name="reset" value="Return to front page">
    </form></p>
    <?php
    
    
    // if admin report has been selected then get infor from table and report.
    if(isset($admin)){
    echo"<h3><center>Users viewing reports</center></h3>";
    ?>
    <form method="POST" action="">
    <p>Please choose the assessment to view users utilising reports</p>
    	<?php
    $sql="SELECT quizid FROM qreports";
    $sql=mysql_query($sql) or die ('Invalid query: ' . mysql_error());
    while($row=mysql_fetch_array($sql)){
    	$admin_quiz[]=$row['quizid'];
    	}
    echo'<select name="admin_report">';
    $admin_quiz=array_unique($admin_quiz);
    foreach($admin_quiz as $value){
    	$admin_name=Get_Quiz($value);
    	echo'<option value="' . $value . '">' . $admin_name . '</option>';
    }
    echo"</select>";
    echo'<input type="submit">';
    echo"</form>";
    unset($admin);
    }elseif(isset($admin_report)){
    $sql="SELECT * FROM qreports WHERE quizid = " . $admin_report;
    $sql=mysql_query($sql);
    while($row=mysql_fetch_array($sql)){
    	$admin_output[$row['id']][$row['username']]=$row['date'];
    }?>
    <table class="outer">
    <tr>
    <td class="outer">User</td><td class="outer">Date of Access</td>
    </tr>
    <?php
    foreach($admin_output as $value){
    	while(list($name,$date)=each($value)){
    	echo '<tr><td class="outer">' . $name . '</td><td class="outer">' . $date . '</td></tr>';
    	}
    }
    ?>
    </table>
    <?php
    	unset ($admin, $admin_report);
    }else{
    
    
    //User Selects quiz title from dropdown to report on.
    		if(!isset($quizid)){
    			echo '<form method="POST" action=""><p>Please select the assessment you which to report from</p><select name="quizid">';
    			$sql="SELECT id, name FROM mdl_quiz WHERE course = 236";
    			$sql=mysql_query($sql);
    			while($row=mysql_fetch_array($sql)){
    				$quiz_id[$row['id']]=$row['name'];
    			}
    			while(list($id, $name)=each($quiz_id)){
    				echo'<option value="' . $id . '">' . $name . "</option>";
    			}
    			echo'</select><input type="submit"></form>';
    
    		}else{
    ?>
    <!--Heading-->
    <p>
    Following on from the <?php echo Get_Quiz($quizid); ?> training we have created an assessment to establish the knowledge and understanding of relevant scenarios within the business.</p>
    <p>
    ALL the results have now been published here.</p>
    <p>
    Please select which report you would like to see from the drop-down below.<br>
    The Regional and ALL UK reports will show the breakdown question by question, which should help establish where specific training requirements are needed across the UK.<br>
    The overview option provides a comparisson against the average score for the UK and the facility to see scores on a delegate by delegate basis.</p>
    
    
    
    
    <?php
    //add to table to track user viewing reports
    $names = Get_Names($USERID);
    $today=nicedatetime($today);
    if ($counter==1){
    if(table_exists("qreports")){
    	$sql="INSERT INTO qreports (userid, username, date, quizid) VALUES ('" . $USERID . "',' " . $names . "',' " . $today . "', ' " . $quizid . "')";
    	mysql_query ($sql) or die ('Invalid query: ' . mysql_error());
    }
    else{
    	$sql="CREATE TABLE qreports (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, userid INT NOT NULL, username VARCHAR(255), date VARCHAR(255), quizid INT NOT NULL)";
    	mysql_query($sql) or die ('Invalid query: ' . mysql_error());
    
    	$sql="INSERT INTO qreports (userid, username, date, quizid) VALUES ('" . $USERID . "',' " . $names . "',' " . $today . "', ' " . $quizid . "')";
    	mysql_query ($sql) or die ('Invalid query: ' . mysql_error());
    
    }
    
    }
    
    		}	
    
    //has user selected individual user?  if so, produce their quiz breakdown
    
    if(isset($UserID)){
    //if (isset($export_user)){
    	//echo $UserID;
    $output_name=Get_Names($UserID);
    $output_score=Get_Score($UserID, $quizid);
    include("get_info.php");
    $sql="SELECT uniqueid FROM mdl_quiz_attempts WHERE quiz = " . $quizid . " AND preview = 0 AND timefinish != 0 and userid=" . $UserID;
    $sql=mysql_query($sql);
    while($row=mysql_fetch_array($sql)){
    	$User_completed=$row['uniqueid'];
    }
    
    $sql="SELECT answer FROM mdl_question_states WHERE attempt = " . $User_completed . " and seq_number = 1";
    $sql=mysql_query($sql);
    while($row=mysql_fetch_array($sql)){
    	$array=explode(":",$row['answer']);
    		$answers_given[]=$array[1];
    }
    
    $count_answers = array_count_values($answers_given);
    echo"<h3><center><u>User Responses for " .  ucwords($output_name) . ".<br>Total Score= $output_score / 30</u></center></h3>";
    echo '<table class="outer"><tr><td><table class="outer" width="100%"><tr><td class="outer" width="40%">Question Text</td><td class="outer" width="30%">Possible Answers</td><td class="outer" width="10%">Correct/Incorrect</td><td class="outer" width="10%">User Response</td></tr></table></td></tr>';
    while(list($qref,$aref_arr)=each($ref_array)){
    echo'<tr class="internal"><td class="outer"><table class"inner" width="100%"><tr></tr><tr><td width="40%">' . $questiontext[$qref] . "</td>";
    while(list($aref,$mark)=each($aref_arr)){
    	if (!isset($count_answers[$aref])){
    		$count_answers[$aref]=0;
    	}
    	if($count_answers[$aref]==0){
    		$response="";
    	}elseif($count_answers[$aref]==1){
    		$response='<h5><span style="font-family: wingdings; font-size: 200%;">&#252;</span></h5>';
    	}
    	echo'<td class="inner" width="30%">' . $answertext[$aref] . '</td>' . $mark . '</style></td><td class="inner" width="10%">' . $response . '</td></tr><tr><td></td>';
    }echo'</table></td></tr><tr class="base"><td>&nbsp</td></tr><tr></tr>';
    }
    echo "</tr></table>";
    unset($UserID);
    }
    //}
    
    
    
    
    
    
    
    
    
    //Does user require export of users for specific region?
    
    elseif(isset($User_region)){
    $SQL="SELECT userid, sumgrades FROM mdl_quiz_attempts WHERE quiz = " . $quizid . " AND preview = 0 AND timefinish != 0";
    $SQL=mysql_query($SQL) or die;
    while($ROW = mysql_fetch_array($SQL)){
    	if($User_region!="No Region Set"){
    		$sql="SELECT data FROM mdl_user_info_data where userid = " . $ROW['userid'];
    		$sql=mysql_query($sql);
    		while($row=mysql_fetch_array($sql)){
    			if ($row['data']==$User_region){
    				$moresql="SELECT firstname, lastname FROM mdl_user where id = " . $ROW['userid'];
    				$moresql=mysql_query($moresql);
    				while($last=mysql_fetch_array($moresql)){
    					$last['firstname']=ucwords($last['firstname']);
    					$last['lastname']=ucwords($last['lastname']);
    					$gradout[$ROW['userid']] = $ROW['sumgrades'];;
    				}
    			}
    		}
    	}else{
    		$sql='SELECT data FROM mdl_user_info_data WHERE userid = ' . $ROW['userid'];
    		$sql=mysql_query($sql);
    		$return=mysql_num_rows($sql);
    		if($return==0){
    			$moresql="SELECT firstname, lastname FROM mdl_user where id = " . $ROW['userid'];
    			$moresql=mysql_query($moresql);
    			while($last=mysql_fetch_array($moresql)){
    				$last['firstname']=ucwords($last['firstname']);
    				$last['lastname']=ucwords($last['lastname']);
    				$gradout[$ROW['userid']] = $ROW['sumgrades'];;
    			}
    		}
    		while($row=mysql_fetch_array($sql)){
    			if (strlen($row['data'])<1){
    				$moresql="SELECT firstname, lastname FROM mdl_user where id = " . $ROW['userid'];
    				$moresql=mysql_query($moresql);
    				while($last=mysql_fetch_array($moresql)){
    					$last['firstname']=ucwords($last['firstname']);
    					$last['lastname']=ucwords($last['lastname']);
    					$gradout[$ROW['userid']] = $ROW['sumgrades'];
    				}
    			}
    		}
    	}		
    }
    arsort($gradout);?>
    <form method="POST" Action="">
    <p><h3>Individual scores for the <?php echo"$User_region"; ?> Region</h3></p>
    <p>The current UK average score for this assessment is <span class="alone"><b><u><?php echo $_SESSION['UK_AVE']; ?></b></u></span></p><p><table ><td><table class="outer"><tr><td class="inner">Name</td><td class="inner">Score out<br>of 30</td><td class="inner">Select to view<br>individual results</td></tr>
    <?php
    	while(list($Userid,$score)=each($gradout)){
    		if($score>=$_SESSION['UK_AVE']){
    			$format='class="over"';
    		}else{
    			$format='class="under"';}
    			$VALUE = Get_Names($Userid);
    		echo '<tr ' . $format . '><td class="inner">' . ucwords($VALUE) . '</td><td class="inner">' . $score . '</td><td class="inner">'?>
    		<input type="radio" name="userid" value="<?php echo $Userid; ?>"></td></tr><?php
    	}
    	echo'</table></td><td valign=top><input type="submit" value="Retrieve User Information"></td></table>';
    	echo'<input type="hidden" name="region" value="' . $region . '"><input type="hidden" name="user_region" value="' . $User_region . '"><input type="hidden" name="quizid" value="' . $quizid . '"><input type="hidden" name="counter" value="' . $counter . '"><input type="hidden" name="quizname" value="' . $quizname . '"></form></p>';
    	unset($User_region);
    }
    
    
    //Has Region been selected?
    elseif (!isset($region) and isset($quizid)){
    
    //POST regions available from DB
    $sql="SELECT param1 FROM mdl_user_info_field WHERE id = 1";
    $sql=mysql_query($sql);
    while ($row=mysql_fetch_array($sql)){
    	$regionlist=explode("\n",$row['param1']);
    }?>
    
    <form method="POST" action="">
    <select name="region">
    <option value="overview">Overview</option>
    <?php foreach($regionlist as $value){?>	
    <option value="<?php echo $value; ?>"><?php echo $value; ?></option>
    <?php } ?>
    <option value="all">All UK Regions</option>
    </select>
    <?php echo'<input type="hidden" name="counter" value="' . $counter . '"><input type="hidden" name="quizid" value="' . $quizid . '">';?>
    <input type="submit">
    <?php
    
    
    
    }elseif(isset($region)){ 
    include("get_info.php");
    
    
    
    if ($region!="overview"){
    
    //POST all users for specified region then check if 'ALL' have been selected
    
    if ($region!="all"){
    $sql='SELECT userid FROM mdl_user_info_data WHERE data = "' . $region . '"';
    $sql=mysql_query($sql);
    while($row=mysql_fetch_array($sql)){
    	$SQL="SELECT uniqueid FROM mdl_quiz_attempts WHERE quiz = " . $quizid . " AND userid = " . $row['userid'] . " AND preview = 0 AND timefinish != 0";
    	$SQL=mysql_query($SQL);
    	while($ROW=mysql_fetch_array($SQL)){
    		$Users_completed[$row['userid']]=$ROW['uniqueid'];
    		$regcount++;
    	}
    }
    }else{
    $sql="SELECT userid, uniqueid FROM mdl_quiz_attempts WHERE quiz = " . $quizid . " AND preview = 0 AND timefinish != 0";
    $sql=mysql_query($sql);
    while($row=mysql_fetch_array($sql)){
    	$Users_completed[$row['userid']]=$row['uniqueid'];
    }
    }
    
    
    //POST all answers given for all users given
    
    foreach ($Users_completed as $value){
    //echo "$value <br>";
    $sql="SELECT answer FROM mdl_question_states WHERE attempt = " . $value . " and seq_number = 1";
    $sql=mysql_query($sql);
    while($row=mysql_fetch_array($sql)){
    	$array=explode(":",$row['answer']);
    		$answers_given[]=$array[1];
    }
    }
    
    
    //find out how many are right and how many are wrong
    
    $count_answers = array_count_values($answers_given);
    
    //count total number of people completing the module
    
    $total = count($Users_completed);
    
    //echo out questions, answers and correct or not
    
    ?>
    
    <p><h3><center>For the <?php echo "$region Region, $total"; ?> Users have completed the Fleet Assessment.</center></h3></p>
    <p>There are 30 questions, each with 4 possible answers.  The following gives a breakdown of how many selected each option and whether the responses given were correct or incorrect.</p>
    <?php
    echo '<table class="outer"><tr><td><table class="outer" width="100%"><tr><td class="outer" width="40%">Question Text</td><td class="outer" width="30%">Possible Answers</td><td class="outer" width="10%">Correct/Incorrect</td><td class="outer" width="10%">Users Responding</td></tr></table></td></tr>';
    while(list($qref,$aref_arr)=each($ref_array)){
    echo'<tr class="internal"><td class="outer"><table class"inner" width="100%"><tr></tr><tr><td width="40%">' . $questiontext[$qref] . "</td>";
    while(list($aref,$mark)=each($aref_arr)){
    	if (!isset($count_answers[$aref])){
    		$count_answers[$aref]=0;
    	}
    	echo'<td class="inner" width="30%">' . $answertext[$aref] . '</td>' . $mark . '</style></td><td class="inner" width="10%">' . $count_answers[$aref] . '</td></tr><tr><td></td>';
    }echo'</table></td></tr><tr class="base"><td>&nbsp</td></tr><tr></tr>';
    }
    echo "</tr></table>";
    }
    else{
    //POST all users completing quiz and their scores
    $SQL="SELECT userid, sumgrades FROM mdl_quiz_attempts WHERE quiz = " . $quizid . " AND preview = 0 AND timefinish != 0";
    $SQL=mysql_query($SQL) or die;
    while($ROW = mysql_fetch_array($SQL)){
    	$sql="SELECT data FROM mdl_user_info_data where userid = " . $ROW['userid'];
    	$sql=mysql_query($sql);
    	$return = mysql_num_rows($sql);
    	if($return==0){
    		$grade['No Region Set'][$ROW['userid']] = $ROW['sumgrades'];
    		$ave_grade['No Region Set'][$ROW['userid']] = $ROW['sumgrades'];
    	}else{
    		while($row=mysql_fetch_array($sql)){
    			$length=strlen($row['data']);
    			if ($length == 0){
    				$grade['No Region Set'][$ROW['userid']] = $ROW['sumgrades'];
    				$ave_grade['No Region Set'][$ROW['userid']] = $ROW['sumgrades'];
    			}else{
    				$ave_grade[$row['data']][$ROW['userid']] = $ROW['sumgrades'];
    				$grade[$row['data']][$ROW['userid']] = $ROW['sumgrades'];
    			}
    		}
    	}
    }
    
    //count for each region
    while(list($location, $number)=each($ave_grade)){
    $total=count($number);
    
    	ksort($number);
    	unset($total_grade);
    	while(list($Userid, $result)=each($number)){
    		$uk_total+=$result;
    		$total_grade+=$result;
    		$COUNT++;
    			}
    	$_SESSION['UK_AVE'] = round(($uk_total/$COUNT),2);
    }
    ?>
    <form method="POST" action=""><table><tr><td><h4>Regions Ranked by Average Score</h4><td></td><td><h4>Regions Ranked by Users Completed</h4></td></tr><tr><td><table border=1><tr><td>Region</td><td>Users<br> Complete</td><td>Average<br>Score</td><td>Select to<br>export users</td></tr>
    <?php
    while(list($location, $number)=each($grade)){
    $total=count($number);
    
    	ksort($number);
    	unset($total_grade);
    	while(list($Userid, $result)=each($number)){
    		$uk_total+=$result;
    		$total_grade+=$result;
    
    
    	}
    	$score=round(($total_grade/$total),2);
    
    	$scoreout[$score][$location]=$total;
    	$Userout[$total][$location]=$score;
    
    
    	}
    	krsort($scoreout);
    	while(list($scores, $value)=each($scoreout)){
    		while(list($place,$Users)=each($value)){
    			if($scores>=$_SESSION['UK_AVE']){
    				$format=' class="over"';
    			}else{
    				$format=' class="under"';
    			}
    			echo"<tr><td $format>$place</td><td$format> $Users</td><td $format>$scores</td><td>"?><input type="radio" name="user_region" value="<?php echo $place; ?>"></td></tr><?php;
    		}
    	}
    echo"<tr><td>UK Average</td><td>$COUNT</td><td>" . $_SESSION['UK_AVE'] . "</td><td>"?><input type="submit"></tr></table></td><td width=20%>&nbsp</td><td width="40%"><table border=1><tr><td>Region</td><td>Users<br> Complete</td><td>Average<br>Score</td><td>Select to<br>export users</td></tr>
    <?php
    krsort($Userout);
    	while(list($Users, $value)=each($Userout)){
    		while(list($place,$scores)=each($value)){
    			if($scores>=$_SESSION['UK_AVE']){
    				$format=' class="over"';
    			}else{
    				$format=' class="under"';
    			}
    			echo"<tr><td $format>$place</td><td$format> $Users</td><td $format>$scores</td><td>"?><input type="radio" name="user_region" value="<?php echo $place; ?>"></tr><?php;
    		}
    	}
    echo"<tr><td>UK Average</td><td>$COUNT</td><td>" . $_SESSION['UK_AVE'] . "</td><td>"?><input type="hidden" name="region" value="<?php echo $region; ?> "><input type="submit"><?php echo '<input type="hidden" name="quizid" value="' . $quizid . '"><input type="hidden" name="counter" value="' . $counter . '"><input type="hidden" name="quizname" value="' . $quizname . '">';?>
    
    </td></tr></form></table></td></table>
    
    <?php
    }
    }
    }
    }else{?>
    <h3><center>You must be a Territory Manager or Regional Director to view these reports<br>
    <form method="POST" action="/../">
    <input type="submit" value="Go to SKooch">
    </form>
    </center></h3>
    <?php }
    
    }else{?>
    <h3><center>You must be logged into SKooch to view this page<br>
    <form method="POST" action="/../">
    <input type="submit" value="Go to SKooch">
    </form>
    </center></h3>
    <?php }
    
    
    
    
    ?>
    
    </BODY>
    </HTML>

  8. ok, imagine you are going through your while loop one step at a time.

     

     

    1st result from query is quiz1, therefore within the loop $_SESSION['quizTitle'] = "quiz1"  echo $_SESSION['quizTitle'] // gives quiz1

    2nd result from query is quiz2, therefore within the loop $_SESSION['quizTitle'] = "quiz2"  echo $_SESSION['quizTitle'] // gives quiz (replacing previous value of $_SESSION['quizTitle']

    .

    .

    .

    .

    And so on every time you loop.  It will simply replace the previous value.

     

    If you create an array as I descriped, you can use a foreach to get the individual values back out.

     

     <?php
    foreach ($_SESSION['quiztitle'] as $value){
    //Result is a block of HTML
    $sqlchk = "SELECT Result FROM quiz WHERE quizTitle = '$value'";
    $rschk = mysql_query($sqlchk, $conn);
    $rowchk = mysql_fetch_array($rschk, MYSQL_ASSOC);
    echo (stripslashes($rowchk['Result']));?>

  9. This is because within the while loop you are just writing over the existing variable each time it loops. 

     

    It may be worth having an array such as $_SESSION['quiztitle'][]=$quizTitle.

     

    This will create a new entry into an array for you which you can reference as you please.

  10. Hi

     

    I have a series of numbers which I am putting into an array, however I need them all to be 5 digits long.  I want to pad out the left side of the shorter ones with zeroes and the easiest way to do this should be sprintf().

     

    However, I've been reading and rereading various tutorials and manuals (inc php.net) and don't really understand how to achieve this.

     

    I thought it would be $formatted=sprintf("%05s", $unformatted);  however this doesn't work.

     

    Hopefully a php guru or just someone who understands it can give me a quick repsonse to this.

     

    Cheers

  11. Hi

     

    I have the following script (unfinished so please no nagging about security breaches!!)  and I want a simple $_SESSION variable to be set and passed abck through if the form is not completed or it is the first time through.  However I am getting no output from $_SESSION['output'].

     

    Can anyone spot where I've gone wrong?

     

    <?
    session_start();
    include_once('connect.php');
    include_once('functions.php');
    
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    
    if(strlen($_POST['ED'])>1){
    $ED=$_POST['ED'];
    }
    
    if(strlen($_POST['password'])>1){
    $password=$_POST['password'];
    }
    
    if(strlen($_POST['firstname'])>1){
    $firstname=$_POST['firstname'];
    }
    
    if(strlen($_POST['lastname'])>1){
    $lastname=$_POST['lastname'];
    }
    
    if(strlen($_POST['email'])>1){
    $email=$_POST['email'];
    }
    
    if(strlen($_POST['dept'])>1){
    $dept=$_POST['dept'];
    }
    
    if(strlen($_POST['workplace'])>1){
    $workplace=$_POST['workplace'];
    }
    
    if(strlen($_POST['home'])>1){
    $home=$_POST['home'];
    }
    
    if(strlen($_POST['role'])>1){
    $role=$_POST['role'];
    }
    
    //create user table and write 1st admin into user table
    if(isset($_POST['change'])){
    if(isset($ED, $password, $firstname, $lastname, $email, $dept, $workplace, $home, $role)){
    	//create table
    	$sql="CREATE TABLE pfp_user (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, ed VARCHAR(255) NOT NULL, firstname VARCHAR(255) NOT NULL, lastname VARCHAR(255) NOT NULL, dept VARCHAR(255)NOT NULL, workplace VARCHAR(255)NOT NULL, home VARCHAR(255)NOT NULL, password VARCHAR(255) NOT NULL, email VARCHAR(255)NOT NULL, role VARCHAR(255) NOT NULL) DEFAULT CHARACTER SET utf8";
    	mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
    
    	//insert data
    	//hash password
    	$password=md5($password);
    	$sql="INSERT INTO pfp_user (ED, firstname, lastname, dept, workplace, home, password, email, role) VALUES ('$ED', '$firstname', '$lastname', '$dept', '$workplace', '$home', '$password', '$email', '$role')";
    		mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
    		$_SESSION['output']="User Successfully Registered.  Please log in.";
    		$extra = 'index.php';
    		header("Location: http://$host$uri/$extra");
    
    
    }else{$_SESSION['output']='<div class="pagetext">You have missed some information<br>Please try again</div>';}
    }
    
    else{
    $_SESSION['output']='<div class="pagetext">As you are the first on this site, would you will be set up as an admin user<br>Please complete your details below</div>';
    }
    
    
    
    
    //Create form to register the first admin user -->
    include_once('header.php');
    echo $_SESSION['output'];
    ?>
    
    <!-- Create form to register the first admin user -->
    
    
    <div class="logintable"><table>
    <form method="POST" action="">
    <tr><td>Payroll Number - </td><td><input type="text" name="ED" value="<?php echo $ED; ?>"></td></tr>
    <tr><td>Password - </td><td><input type="password" name="password" ></td></tr>
    <tr><td>Firstname - </td><td><input type="text" name="firstname" value="<?php echo $firstname; ?>"></td></tr>
    <tr><td>Surname - </td><td><input type="text" name="lastname" value="<?php echo $lastname; ?>"></td></tr>
    <tr><td>Email - </td><td><input type="text" name="email" value="<?php echo $email; ?>"></td></tr>
    <tr><td>Department - </td><td><input type="text" name="dept" value="<?php echo $dept; ?>"></td></tr>
    <tr><td>Permanent Workplace - </td><td><input type="text" name="workplace" value="<?php echo $workplace; ?>"></td></tr>
    <tr><td>Home Town - </td><td><input type="text" name="home" value="<?php echo $home; ?>"></td></tr>
    <tr><td>Role - </td><td><input type="hidden" name="role" value="admin">Admin</td></tr>
    <tr><td colspan="2" align="center"><input type="submit" name="change" value="Submit"></td></tr>
    </form></table></div>
    

  12. Hi

     

    I have created an image using the image() functions of php and have saved it to the my server.  I now need my script to be able to delete and overwrite this file, but due to the server settings (which I have no control of) every file saved becomes read-only (644) I can access the files and change this via ftp, but this is obviously not an option when my app becomes live online.  Is there a way I can change the attributes of the file to (777)?

     

     

  13. That's right.  But on second thoughts, this is an app to dynamically create a report from a series of 80 questions which fall into various categories,  including graphs and output to the browser, so possibly to save the graphic and then add to an html page would be better.

     

     

  14. Hi Guys

     

    Thanks for the help, but $_POST['questiontext'] is set by the form around line 126.  I know the database is connected as the form populates information from there prior to submission. 

     

    The problem is not with the variable $questiontext, this will be added to the code later once I can find out why the escape function will not work with $dirty_new_text and $new_text.

     

    As mentioned I have manually set a value to $ new_text, and it works until I pass it to the mysql_real_escape_string function.

     

    Any more ideas?

  15. Hi All

     

    I'm trying to create a 'safe' string from a user input to be written to a database, however when I use the mysql_real_escape_string function I just get an empty variable returned.  I've even tried hard-coding the variable and it just gets wiped.

     

    <?php
    session_start();
    include_once("connect.php");
    include_once("is_logged_in.php");
    include_once("is_admin.php");
    
    $USERID=$_SESSION['USERID'];
    $new_question=$_POST['new_question'];
    $dirty_questiontext=$_POST['questiontext'];
    $dirty_new_text=$_POST['new_text'];
    $new_id=$_POST['new_id'];
    $type=$_POST['type'];
    $new_text=mysql_real_escape_string($dirty_new_text);
    echo "this is the new stuff" . $new_text;
    
    
    //get custom functions
    include_once("functions.php");
    
    
    
    //check data tables exist and create if not
    include("table_check.php");
    
    
    
    
    // add any new questiontext
    
    if(isset($questiontext)){
    $sql="INSERT INTO ls_questions (text, category) VALUES ('$questiontext', '$type')";
    mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
    unset($new_question);
    unset($questiontext);
    unset($new_text);
    unset($new_id);
    }
    
    //any questions edited?
    /*if(isset($new_text)){
    $sql='UPDATE ls_questions SET text = "' . $new_text . '",category= "' . $type . '" WHERE id = ' . $new_id;
    mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
    unset($new_question);
    unset($questiontext);
    unset($new_text);
    unset($new_id);
    }
    */
    
    
    
    //is user an admin and shall we add / edit question text?
    include("header.php");
    echo"</head><body>";
    if ($_SESSION['role']=="admin"){
    //has user selected to add a question?
    if(isset($new_question)){
    	if($new_question=="create"){
    	?>
    	<p>Add text of new question</p>
    	<form method="POST" action="">
    	<input type="text" name="questiontext" size="100">
    	<select name="type">
    		<option value="Activist">Activist</option>
    		<option value="Reflector">Reflector</option>
    		<option value="Theorist">Theorist</option>
    		<option value="Pragmatist">Pragmatist</option>
    		</select>
    	<input type="submit">
    	</form>
    	<?php
    }else{
    		$text=get_question_text($new_question, "ls_questions");
    		echo"<p>Previous question text is $text</p><p>Please enter new text in box below</p>";
    		?>
    		<form method="POST" action="">
    		<input type="text" name="new_text" size="100" value="<?php echo $text; ?>">
    		<input type="hidden" name="new_id" value="<?php echo $new_question; ?>">
    		<select name="type">
    		<option value="Activist">Activist</option>
    		<option value="Reflector">Reflector</option>
    		<option value="Theorist">Theorist</option>
    		<option value="Pragmatist">Pragmatist</option>
    		</select>
    		<input type="submit">
    		<?
    	}
    }
    
    echo"<p>Below is a list of current questions, either select one to edit or add a new one</p>";
    
    //get all current questions and put in array with id numbers as key
    
    $sql="SELECT id, text, category FROM ls_questions";
    $sql=mysql_query($sql);
    $count=mysql_num_rows($sql);
    while($row=mysql_fetch_array($sql)){
    	$all_questions[$row['id']][$row['category']]=$row['text'];
    }
    //echo "<H1>count - $count</H1>";
    
    //echo out existing questions as list with a radio button to edit
    if($count!=0){
    ?>
    <table border="2">
    <form method="POST" action="">
    <tr><td>Question Text</td><td>Type</td><td>Edit?</td><tr>
    <tr><td colspan="2">Add new question</td><td><input type="radio" name="new_question" value="create"></td></tr>
    
    
    <?php
    	while(list($id, $array)=each($all_questions)){
    	while(list($category, $text)=each($array)){
    	echo'<tr><td>' . $text . '</td><td>' . $category . '</td><td><input type="radio" name="new_question" value="' . $id . '"</td></tr>';
    }
    }
    ?>
    <tr><td colspan="3" align="center"><input type="submit"></td></tr>
    <form>
    </table>
    <?php
    }else{
    	?>
    	<p>Add text of first question</p>
    	<form method="POST" action="">
    	<input type="text" name="questiontext" size="100">
    	<select name="type">
    		<option value="Activist">Activist</option>
    		<option value="Reflector">Reflector</option>
    		<option value="Theorist">Theorist</option>
    		<option value="Pragmatist">Pragmatist</option>
    		</select>
    	<input type="submit">
    	</form>
    	<?php
    	}
    
    }else{
    echo "fail";
    }
    
    ?>
    </body>

     

    Any ideas?

  16. Hi

     

    I have created the following file to output results from a questionnaire in a visual way.  This was working absolutely fine, but now has decided to stop working.  All I did was navigate to a different web page.  I have tried closing my browser and clearing cache etc but still it won't output to a web page.

     

    I get the windows pop-up of 'Open, Save or Cancel'  If I chose open, it simply opens the file in notepad.

     

    Any ideas?

     

    <?php
    
    //Create image size and initiate
    $image=imagecreate(1000,1000);
    
    //Allocate colours (1st is always background)
    
    $background=imagecolorallocate($image, 255, 255, 255);
    $white = imagecolorallocate($image, 255, 255, 255);
    $black = imagecolorallocate($image, 0, 0, 0);
    $red = imagecolorallocate($image, 255, 0, 0);
    
    //array of results
    $activist=10;
    $reflector=18;
    $theorist=17;
    $pragmatist=9;
    
    //set zeros for x & y
    $x=500;
    $y=500;
    
    //change results into coordinates
    $values=array(
    $x, $y-($activist*20),
    $x+($reflector*20), $y,
    $x, $y+($theorist*20),
    $x-($pragmatist*20), $y);
    
    
    
    
    //create vertices
    //x axis
    imageline($image, 500, 100, 500, 900, $black);
    
    //y axis
    imageline($image, 100, 500, 900, 500, $black);
    
    // create picture
    
    imagefilledpolygon($image, $values, 4, $black);
    //change to outline
    
    
    //add titles
    
    // Path to our ttf font file
    $font_file = 'arial.ttf';
    
    //create labels for axis
    imagefttext($image, 20, 0, 450,50,$black, 'arial.ttf',"Activist" );
    imagefttext($image, 20, 0, 450,950,$black, 'arial.ttf',"Theorist" );
    imagefttext($image, 20, 90, 50,550,$black, 'arial.ttf',"Pragmatist" );
    imagefttext($image, 20, 90, 950,550,$black, 'arial.ttf',"Reflector" );
    
    //create labels for co-ordinates
    imagefttext($image, 20, 0, $x, $y-($activist*20)-50,$red, 'arial.ttf',"$activist" );
    imagefttext($image, 20, 0, $x, $y+($theorist*20)+50,$red, 'arial.ttf',"$theorist" );
    imagefttext($image, 20, 0, $x-($pragmatist*20)-50,$y,$red, 'arial.ttf',"$pragmatist" );
    imagefttext($image, 20, 0, $x+($reflector*20)+50,$y,$red, 'arial.ttf',"$reflector" );
    
    //set header
    header("content-type:image.png");
    
    //produce image
    
    imagepng($image);
    
    
    
    
    ?>
    

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