Error with php field validation - php

I have a form on a PHP page which adds a contact into a MySQL table.
But my validation refuses to work. I have edited it a few times but when I click Add New Contact button, it adds them even if some fields are empty.
I am still getting the grasp of PHP, but I am sure my mistake is missing something simple.
<?php
// Don't post the form until the submit button is pressed.
$requiredFields = array (
"name",
"email",
"extension",
"extension"
); // Add the 'name' for all required fields to this array
$errors = false;
if (isset ( $_POST ['Submit'] )) {
// Clean all inputs
array_walk ( $_POST, 'check_input' );
// Loop over requiredFields and output error if any are empty
foreach ( $requiredFields as $r ) {
if (strlen ( $_POST [$r] ) == 0) {
$errors = true;
break;
}
}
// Error/success check
if ($errors == true) {
echo 'Fields marked with a * are required';
} else {
// no errors
// ...
}
}
// check_input function
function check_input(&$data) {
$data = trim ( $data );
$data = stripslashes ( $data );
$data = htmlspecialchars ( $data, ENT_QUOTES );
return $data;
}
?>
<h2>Add Contact</h2>
<form name="form1" action="<?=$_SERVER['PHP_SELF'];?>?mode=added"
method="post">
<table class="tableStyleClassTwo">
<tr>
<td>Name:</td>
<td><div align="left">
<input type="text" name="name" />
</div></td>
</tr>
<tr>
<td>Phone:</td>
<td><div align="left">
<input type="text" name="phone" />
</div></td>
</tr>
<tr>
<td>Email:</td>
<td><div align="left">
<input type="text" name="email" />
</div></td>
</tr>
<tr>
<td>Extension:</td>
<td><div align="left">
<input type="text" name="extension" />
</div></td>
</tr>
<tr>
<td>Department:</td>
<td><select name="department">
<option value="ADMIN">ADMIN</option>
<option value="AFTER-SALES DIRECTOR">AFTER-SALES DIRECTOR</option>
<option value="ALPINE DEALER PRINCIPAL">ALPINE DEALER PRINCIPAL</option>
<option value="AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)">AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)</option>
<option value="BANDIT-VW">BANDIT-VW</option>
<option value="BOOKINGS VW">BOOKINGS VW</option>
<option value="DRIVEWAY/WASHBAYS">DRIVEWAY/WASHBAYS</option>
<option value="FINANCE AND INSURANCE">FINANCE AND INSURANCE</option>
<option value="IT DEPARTMENT">IT DEPARTMENT</option>
<option value="MARKETING DEPARTMENT">MARKETING DEPARTMENT</option>
<option value="MASTER CARS">MASTER CARS</option>
<option value="MAYOR OF PINETOWN">MAYOR OF PINETOWN</option>
<option value="NEW CAR PREP DEPARTMENT">NEW CAR PREP DEPARMENT</option>
<option value="NUMBER PLATES">NUMBER PLATES</option>
<option value="PANELBEATER - EASIFIX - CAR CARE">PANELBEATER - EASIFIX - CAR CARE</option>
<option value="PARTS">PARTS</option>
<option value="PARTS DISPATCH">PARTS DISPATCH</option>
<option value="PARTS TELESALES">PARTS TELLESALES</option>
<option value="USED CAR PREP AND ORDERS">USED CAR PREP AND ORDERS</option>
<option value="VW NEW CARS ADMIN AND STOCK CONTROL">VW NEW CARS ADMIN AND STOCK CONTROL</option>
<option value="VW NEW VEHICLE SHOWROOM">VW NEW VEHICLE SHOWROOM</option>
<option value="VW SERVICE ADVISORS">VW SERVICE ADVISORS</option>
<option value="VW WORKSHOP">VW WORKSHOP</option>
<option value="VW WORKSHOP FOREMEN">VW WORKSHOP FOREMEN</option>
<option value="WARRANTY & CLAIMS">WARRANTY & CLAIMS</option>
<option value="WORKSHOP DRIVERS">WORKSHOP DRIVERS</option>
</select></td>
</tr>
<tr>
<td colspan="2" align="centre">Back
| <input name="Submit" type="submit" id="Submit" value="Add New Contact" <?php if($disable ==1){?> disabled <?php } ?> />
</td>
</tr>
<input type="hidden" name="mode" value="added">
</table>
</form>
<?php
break;
//added a record
case 'added':
//first setup the vars
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$extension = $_POST['extension'];
$department = $_POST['department'];
//then lets use'em
$sql = "INSERT INTO address (name, phone, email, extension, department) VALUES ('" . $name . "','" . $phone . "','" . $email . "','" . $extension . "','" . $department . "')";
//echo $sql;
//return;
mysql_query($sql);
//done take me back to the main page
header('location: ' . $_SERVER['PHP_SELF']);
break;

Related

mysql and php %like% query. Works in phpmyadmin but different results php

I have some search fields that look across a few different tables.
"display_name" form field and "last" form field show different results then what it does directly into phpmyadmin.
If i echo out the mysql query in my php script and paste it into phpmyadmin. It lists of correct results. However on the php/html page it is not listing the same.
For example. If someones name full name is nathan spencer and i put spencer into the "last" form field it will only show 1 result or 2 results. HOWEVER there are actually 5 results found by pasting it directly into phpmyadmin and running it.
I have been battling for ages with this and its driving me nuts.
Here is the PHP up the top of the page:
<?php
// SEARCH
if(isset($_POST['submit'])) {
// define the list of fields
$fields = array('display_name', 'last', 'suburb', 'state', 'user_type', 'active');
$conditions = array();
// loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && $_POST[$field] != '') {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$conditions[] = "`$field` LIKE '%".mysql_real_escape_string($_POST[$field])."%'";
}
}
// builds the query
$query = "SELECT display_name, first, last, suburb, state, user_type, active FROM nfw_users ";
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= "WHERE " . implode (' AND ', $conditions) .""; // you can change to 'OR', but I suggest to apply the filters cumulative
}
else {
echo "No records found";
}
$result = mysql_query($query);
$score = mysql_fetch_assoc($result);
}
?>
and here is the html form
<form method="post" action="index.php">
<tr>
<td>Name:</td>
<td><input type="text" name="display_name" /></td>
</tr>
<tr>
<td>Street:</td>
<td><input type="text" name="last" /></td>
</tr>
<tr>
<td>Suburb:</td>
<td><input type="text" name="suburb" /></td>
</tr>
<tr>
<td>State:</td>
<td>
<select name="state">
<option>
<option value="qld">QLD</option>
<option value="sa">SA</option>
<option value="nt">NT</option>
<option value="wa">WA</option>
<option value="vic">VIC</option>
<option value="tas">TAS</option>
<option value="act">ACT</option>
</select>
</td>
</tr>
<tr>
<td>Type:</td>
<td>
<select name="user_type">
<option>
<option value="franchise">Franchisee</option>
<option value="regional">Regional</option>
<option value="state">State</option>
<option value="national">National</option>
<option value="office">Headoffice Staff</option>
</select>
</td>
</tr>
<tr>
<td>Active:</td>
<td>
<select name="active">
<option></option>
<option value="1">Active</option>
<option value="0">Not Active</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Search" /></td>
</tr>
</form>
<form method="post" action="index.php">
<tr>
<td>Name:</td>
<td><input type="text" name="display_name" /></td>
</tr>
<tr>
<td>Street:</td>
<td><input type="text" name="last" /></td>
</tr>
<tr>
<td>Suburb:</td>
<td><input type="text" name="suburb" /></td>
</tr>
<tr>
<td>State:</td>
<td>
<select name="state">
<option>
<option value="qld">QLD</option>
<option value="sa">SA</option>
<option value="nt">NT</option>
<option value="wa">WA</option>
<option value="vic">VIC</option>
<option value="tas">TAS</option>
<option value="act">ACT</option>
</select>
</td>
</tr>
<tr>
<td>Type:</td>
<td>
<select name="user_type">
<option>
<option value="franchise">Franchisee</option>
<option value="regional">Regional</option>
<option value="state">State</option>
<option value="national">National</option>
<option value="office">Headoffice Staff</option>
</select>
</td>
</tr>
<tr>
<td>Active:</td>
<td>
<select name="active">
<option></option>
<option value="1">Active</option>
<option value="0">Not Active</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Search" /></td>
</tr>
</form>
and here is the results that get printed into a table
<?php if(isset($score)){
while($score=mysql_fetch_assoc($result)){
$display_name = $score['display_name'];
$lastname = $score['last'];
$state = $score['state'];
$active = $score['active'];
if ($active=='1') {
$activeother = "<i class='fa fa-check' style='color:green;'></i>";
}
else {
$activeother = "<i class='fa fa-times' style='color:red;'></i>";
}
?>
<?php
$content = "<tr><td>" . $score['display_name'] . "</td><td>" . $score['first'] . "</td><td>" . $score['last'] . " </td><td>" . $score['email'] . " </td><td> " . $score['mobile'] . " </td><td> " . $score['landline'] . "</td><td>$activeother</td><td> " . $score['user_type'] . "</td><td> " . date('d-m-Y', strtotime($score['date_join'])) . "</td><td class='invoicing-columns'><a class='btn btn-yellow' href='view-invoices.php?id=" . $score['id_num'] . "'><i class='fa fa-eye'></i></a></td><td class='invoicing-columns'><a class='btn btn-red' href='del-customers.php?id=" . $score['id_num'] . "' onclick='return check();' class='delete'><i class='fa fa-minus-circle'></i></a></td></tr>";
echo $content;
}}
?>
If I got your code right there might be issue:
Here you actually fetching first row.
$result = mysql_query($query);
$score = mysql_fetch_assoc($result);
Then it seems that you simply skip it and go with loop:
if(isset($score)){
while($score=mysql_fetch_assoc($result)){
What you really want to do is:
$result = mysql_query($query);
if (!$result) {
//TODO: Query error handling
}
// This is how you check for results count
if (mysql_num_rows($result) == 0) {
while ($score = mysql_fetch_assoc($result)) {
//Here you go with result
}
}
You can also check this manual link as reference.
Next thing to check, get exact string from query and try it from phpMyAdmin. Make sure that you use same user as your code does, when doing query.
And one more thing, extension you are using is long time deprecated, you should consider to switching to MySQLi or PDO_MySQL

Wordpress custom table update not working

I'm trying to update wp_table (custom table) via frontend.
i want update records in self submitted page $_SERVER['PHP_SELF']
after update sucesfully i want redirect user to home page. but not working
$domain = $_POST['domain'];
$appointment = $_POST['appointment'];
$rid = $_POST['rid'];
global $wpdb;
if (!isset($_POST['save'])) {
if (mysql_query("UPDATE wp_domain SET domain='$domain', appointmenttime_setting='$appointment' WHERE re_id=$rid"))
{
//echo "UPDATE wp_domain SET domain='$domain', cron_setting='$twitime' WHERE re_id=$rid";
header( 'Location:http://localhost/thepost/?page_id=17' ) ;
}
}
My html code is here
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<table width="300" border="1">
<tr style="width:450px;">
<td>Domain</td>
<td>
<?php
$act = $Rrow->active_not;
if($act=="NO")
{ ?>
<input name="domain" value="<?php echo $Rrow->domain; ?>" type="text">
<?php
}
else
{
echo $Rrow->domain;
}
?>
</td>
</tr>
<tr>
<td>Appointment Time</td>
<td><select name="appointment">
<option value="5 Minute">5 Minute</option>
<option value="10 Minute">10 Minute</option>
<option value="15 Minute">15 Minute</option>
<option value="30 Minute">30 Minute</option>
<option value="45 Minute">45 Minute</option>
<option value="1 Hour">1 Hour</option>
</select>
<input type="hidden" name="rid" value="<?php echo $Rrow->re_id; ?>" />
</td>
</tr>
<tr>
<td><a class="btn">SAVE</a></td>
<td><input name="save" id="save" type="submit" /></td>
</tr>
</table>
</form>
I feel your issue is with if (!isset($_POST['save'])) and mysql_query instead of wp_query, try like this
if (isset($_POST['save'])) {
if (wp_query("UPDATE wp_domain SET domain='$domain', appointmenttime_setting='$appointment' WHERE re_id=$rid")){
header( 'Location:http://localhost/thepost/?page_id=17' ) ;
exit;
}
}

How to insert the selected data from drop down menu into multiple table in database

This is my register.php (form):
<table width="350" border="0">
<center><tr>
<td><form action="newregister.php" method="post">
Nama</td>
<td> : </td>
<td></td>
<td width="200"><center><input name="nama_pelajar" type="text"></center></td>
</tr>
<tr>
<td>No Kad Pengenalan</td>
<td> : </td>
<td></td>
<td><center><input name="ic_pelajar" type="text"></center></td></tr>
<tr>
<td>ID Pelajar</td>
<td> : </td>
<td></td>
<td><center><input name="id_pelajar" type="text"></td>
</tr>
<tr>
<td><br>Sesi</td>
<td> : </td>
<td></td>
<td><center><select name="sesi">
<option><center>-Sila pilih-</option>
<option value="Jun 14">Jun 2014</option>
<option value="Dis 14">Dis 2014</option>
<option value="Jun 15">Jun 2015</option>
<option value="Dis 15">Dis 2015</option>
<option value="Jun 16">Jun 2016</option>
<option value="Dis 16">Dis 2016</option>
<option value="Jun 17">Jun 2017</option>
<option value="Dis 17">Dis 2017</option>
</select>
</td>
</tr>
<tr>
<td><br>Kursus</td>
<td> : </td>
<td></td>
<td><center><select name="kursus">
<option>-Sila pilih-</option>
<option value="Senibina">Kursus Lukisan Seni Bina</option>
<option value="Elektrik">Kursus Pemasangan Elektrik</option>
<option value="Fesyen">Kursus Fesyen & Pakaian</option>
<option value="Makanan">Kursus Pemprosesan Makanan</option>
</select></td>
</tr>
<tr>
<td><br>No Telefon</td>
<td> : </td>
<td></td>
<td><center><input name="no_tel" type="text"></td>
</tr>
<tr>
<td><br>Email</td>
<td> : </td>
<td></td>
<td><center><input name="email" type="text"></td>
</tr>
<tr>
<td><center><input name="" type="submit" value="Simpan">
<input name="" type="reset" value="Set Semula">
</center>
</form>
</td>
</tr>
</table>
and this is my php code (newregister.php):
<?php
require_once "conn.php";
$conn = connect();
$db = connectdb();
mysql_select_db($db,$conn) or die (mysql_error() . "\n");
$query_usr = "select * from register_jun14";
$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
$row_usr=mysql_fetch_assoc($usr);
$nama_pelajar=$_REQUEST["nama_pelajar"];
$ic_pelajar=$_REQUEST["ic_pelajar"];
$id_pelajar=$_REQUEST["id_pelajar"];
$sesi=$_REQUEST["sesi"];
$kursus=$_REQUEST["kursus"];
$no_tel=$_REQUEST["no_tel"];
$email=$_REQUEST["email"];
$query = "INSERT INTO register_jun14(nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysql_query($query);
echo "<script languange = 'Javascript'>
alert('Pendaftaran berjaya!');
location.href = 'register.php';</script>";
?>
My question:
How to insert selected "sesi" (drop down menu) into new table in database if there have table for "Jun14","Dis14","Jun15","Dis15" and so on ?
Anyone?
I am assuming you want to choose the table (means you have multiple table jun14, dis14, jun15 and so on)according to your drop down selection in this box
<td><center><select name="sesi">
<option><center>-Sila pilih-</option>
<option value="Jun 14">Jun 2014</option>
<option value="Dis 14">Dis 2014</option>
<option value="Jun 15">Jun 2015</option>
<option value="Dis 15">Dis 2015</option>
<option value="Jun 16">Jun 2016</option>
<option value="Dis 16">Dis 2016</option>
<option value="Jun 17">Jun 2017</option>
<option value="Dis 17">Dis 2017</option>
</select>
</td>
first you need to change the above part like this
//you should remove the space and add underscore in order for your query to work
//for the if statement
<td><center><select name="sesi">
<option><center>-Sila pilih-</option>
<option value="jun_14">Jun 2014</option>
<option value="dec_14">Dis 2014</option>
<option value="Jun_15">Jun 2015</option>
<option value="Dis_15">Dis 2015</option>
<option value="Jun_16">Jun 2016</option>
<option value="Dis_16">Dis 2016</option>
<option value="Jun_17">Jun 2017</option>
<option value="Dis_17">Dis 2017</option>
</select>
</td>
i removed the space in the value and added a underscore like jun 14 become jun_14, dec 14 become dec _14 and so on,
now your newregister php should look like this
<?php
//i remove your this line assuming these lines you are connecting with your database these lines i removed
//require_once "conn.php";
//$conn = connect();
// $db = connectdb();
//mysql_select_db($db,$conn) or die (mysql_error() . "\n");
//and i added this line instead in this line i am also connecting
//with database
$conn= mysqli_connect('localhost', 'root', 'yourpassword', 'yourdatabasename')
or die(mysqli_error($con));
$query_usr = "select * from register_jun14";
//$usr = mysql_query($query_usr,$conn) or die(mysql_error()."\n".$query_usr);
$usr = mysqli_query($conn,$query_usr) or die(mysqli_error()."\n".$query_usr);
$row_usr=mysqli_fetch_assoc($usr);
$nama_pelajar=$_REQUEST["nama_pelajar"];
$ic_pelajar=$_REQUEST["ic_pelajar"];
$id_pelajar=$_REQUEST["id_pelajar"];
$sesi=$_REQUEST["sesi"];
$kursus=$_REQUEST["kursus"];
$no_tel=$_REQUEST["no_tel"];
$email=$_REQUEST["email"];
if($sesi=="jun_14")
{
//assuming you have Jun14 table in your database with same field as your
//original insert query in the question
$query = "INSERT INTO Jun14(id, nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('','$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
}
if($sesi=="dec_14")
{
//assuming you have Dis14 table in your database with same field as your
//original insert query in the question
$query = "INSERT INTO Dis14(id, nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('','$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
}
if($sesi=="jun_15")
{
//assuming you have Jun15 table in your database with same field as your
//original insert query in the question
$query = "INSERT INTO Jun15(id, nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('','$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
}
//i am leaving this your query here i dont know why you need it, may be you
//need it
$query = "INSERT INTO register_jun14 ( nama_pelajar,ic_pelajar,id_pelajar,sesi,kursus,no_tel,email) VALUES ('$nama_pelajar','$ic_pelajar',
'$id_pelajar','$sesi','$kursus','$no_tel','$email')";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
you can write further if statement according to your value in drop down menu
You can add as many if condition you want in order to achieve your goal for drop down menu, why you are running select query i have no idea.

Change HTML DropDown Default Value with a MySQL value

I'm working on a profile page, where a registered user can update their information. Because the user has already submitted their information, I would like their information from the database to populate my HTML form.
Within PHP, I'm creating the HTML form with the values filled in. However, I've tried creating an IF statement to determine whether an option is selected as the default value. Right now, my website is giving me a default value of the last option, Undeclared. Therefore, I'm not sure if all IF statements are evaluation as true, or if it is simply skipping to selected=selected.
Here is my HTML, which is currently embedded with PHP(<?php ?>):
<?php
// Connect to MySQL
$db = mysql_connect("", "xxx", "xxx");
if (!$db)
{
exit("Error - Could not connect to MySQL");
}
$er = mysql_select_db("cs329e_fall11_nemo008", $db);
if (!$er)
{
exit("Error - Could not select the database");
}
$query = "SELECT *
FROM tblMembers
WHERE Username = 'fzr11017' ";
$result = mysql_query($query);
if (!$result)
{
print("Error - The query could not be executed");
$error = mysql_error();
print("<p>:" . $error . "</p>");
exit;
}
$row = mysql_fetch_array($result);
print <<<FORM
<form id="frmRegister" action="update.php" method="post" onsubmit="return Validate()" >
<table>
<tr>
<th><br /></th>
<td><br /></td>
</tr>
<tr>
<th align="left">Username:</th>
<td><input type="text" name="Username" maxlength="10" value=$row[Username] readonly="readonly"/></td>
</tr>
<tr>
<th align="left">First Name:</th>
<td><input type="text" name="FirstName" value=$row[FirstName] readonly="readonly" /></td>
</tr>
<tr>
<th align="left">Last Name:</th>
<td><input type="text" name="LastName" value=$row[LastName] readonly="readonly" /></td>
</tr>
<tr>
<th align="left">Email Address:</th>
<td><input type="text" name="Email" value=$row[Email] /></td>
</tr>
<tr>
<th align="left">Phone Number:</th>
<td><input type="text" name="Phone" maxlength="10" value=$row[Phone] /></td>
</tr>
<tr>
<th align="left">Year:</th>
<td>
<select name="Year" >
<option if(strcmp($row[Year], 'Freshman') == 0){ selected="selected"} >Freshman</option>
<option if(strcmp($row[Year], 'Sophomore') == 0){ selected="selected"} >Sophomore</option>
<option if(strcmp($row[Year], 'Junior') == 0){ selected="selected"} >Junior</option>
<option if(strcmp($row[Year], 'Senior') == 0){ selected="selected"} >Senior</option>
</select>
</td>
</tr>
<tr>
<th align="left">Primary Major:</th>
<td>
<select name="Major">
<option if($row[Major] == Accounting){ selected="selected"}>Accounting</option>
<option if($row[Major] == Business Honors Program){ selected="selected"}>Business Honors Program</option>
<option if($row[Major] == Engineering Route to Business){ selected="selected"}>Engineering Route to Business</option>
<option if($row[Major] == Finance){ selected="selected"}>Finance</option>
<option if($row[Major] == International Business){ selected="selected"}>International Business</option>
<option if($row[Major] == Management){ selected="selected"}>Management</option>
<option if($row[Major] == Management Information Systems){ selected="selected"}>Management Information Systems</option>
<option if($row[Major] == Marketing){ selected="selected"}>Marketing</option>
<option if($row[Major] == MPA){ selected="selected"}>MPA</option>
<option if($row[Major] == Supply Chain Management){ selected="selected"}>Supply Chain Management</option>
<option if($row[Major] == Undeclared){ selected="selected"}>Undeclared</option>
</select>
</td>
</tr>
<tr>
<th><br /></th>
<td><br /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="btnSubmit" value="Submit" /></td>
<td align="center"><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
FORM;
?>
You've mixed HTML and PHP without declaring PHP tags and you've also used 'selected' as an attribute...
<select name="Major">
<option <?php echo ($row['Major'] == "Accounting") ? " selected" : ""; ?>>Accounting</option>
....
</select>
I've done the first one, but you can follow the same pattern
That code looks like it could use a towel:
<select name="Major">
<?php
$options = array(
'Accounting'
, 'Business Honors Program'
, 'Engineering Route to Business'
, 'Finance'
, 'International Business'
, 'Management'
, 'Management Information Systems'
, 'Marketing'
, 'MPA'
, 'Supply Chain Management'
, 'Undeclared'
);
foreach( $options as $option )
{
printf(
"<option%s>%s</option>\n"
, ($row['Major'] == $option ? ' selected="selected"' : '')
, htmlentities($option)
);
}
?>
</select>
You might find some of this reading useful to help explain some of the concepts used above:
arrays
foreach loop
ternary operator (?:)
printf()
htmlentities()
You seem to be missing any tags in your code, which means that nothing is being processed. Something more along the lines of this should be used:
<option <?php if($row["Major"] == "Accounting"){ echo "selected"; } ?>>Accounting</option>

how do i apply Criteria::LIMIT for upper and lower or is there another way?

i have this code in indexSucces.php
//some html for the main page
<td width='48%' align='right'>
<?php include_partial('login', array('form' => $form)) ?>
</td>
in the actions.class.php for index i have:
public function executeIndex()
{
$this->form = new RcLoginForm();
$this->setTemplate('index');
$this->search_form = new RcSearchForm();
$this->age_form = new RcAgeTableForm();
}
public function executeListmatches(sfWebRequest $request)
{
}
then the partial _login.php code:
<form action="<?php echo url_for('password/listmatches' ) ?>" method="post" >
<tr>
<td colspan="2">
<span class='spn_med_lightblue_rbc'>I am a:</span>
<select id="gender1" name="gender1">
<option value="male1" >Male</option>
<option value="female1" selected="selected">Female</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<span class='spn_med_lightblue_rbc'>Seeking a:</span>
<select id="gender2" name="gender2">
<option value="male2" >Male</option>
<option value="female2" selected="selected">Female</option>
</select>
</td>
</tr>
<tr>
<td>
<span class='spn_med_lightblue_rbc'>Age:</span>
<select id="age1" name="age1">
<?php $ages = RcAgeTablePeer::getById(18);
foreach ($ages as $age)
{
//echo $age->getId();
echo "<option>";
echo $age->getId();
echo "</option>";
} ?>
</select>
</td>
<td>
<span class='spn_med_lightblue_rbc'>To:</span>
<select id="age2" name="age2">
<?php foreach ($ages as $age)
{
//echo $age->getId();
echo "<option>";
echo $age->getId();
echo "</option>";
} ?>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<span class='spn_med_lightblue_rbc'>Location:</span>
<select id="province" name="province">
<option value="all" selected="selected">All</option>
<option value="ec">Eastern Cape</option>
<option value="nc">Northern Cape</option>
<option value="wc">Western Cape</option>
<option value="fs">Free State</option>
<option value="gp">Gauteng</option>
<option value="kzn">Kzn</option>
<option value="lim">Limpopo</option>
<option value="mpu">Mpumulanga</option>
<option value="nw">North West</option>
</select>
</td>
<td><input class='submit_img' type="image" src="/images/rainbow/gobuttonbluesmall.png" value="Submit" alt="Submit"></td>
</tr>
<tr></tr>
<tr></tr>
</form>
the above will give a home page with a section(the partial _login) that contains the login to the website and also a quick search when you hit the go button the form action will go to
action="
this is my dilemma..i am not in any class, i just pass on the selected values through POST and use those values to get my rows
in listmatchesSuccess.php
$matching_rows = RcProfileTablePeer::getAllBySelection($gender_id2,$age1,$age2,$province_id,$from,$limit);
where above in RcProfileTablePeer.php:
static public function getAllBySelection($gender2,$age1,$age2,$province_id,$from,$limit)
{
$criteria = new Criteria();
$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->add(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
$criteria->add(RcProfileTablePeer::GENDER_ID,$gender2, Criteria::EQUAL);
if ($province_id <> 10)
$criteria->addAnd(RcProfileTablePeer::PROVINCE_ID,$province_id, Criteria::EQUAL);
$criteria->setLimit($limit);
return self::doSelect($criteria);
}
hope all this makes better sense now :)
thank you
I hope I understand what you are asking,
I you want to add a limit to your query
$criteria->setLimit(10);
and also if you want to create a pager in symfony, I would suggest the propel pager class
e.g.
$this->page = $request->getParameter('page', 1);
$c = new Criteria();
$c->add(UserPeer::Status_ID,1);
$this->pager = new sfPropelPager('User', $limit);
$this->pager->setCriteria($c));
$this->pager->setPeerMethod('doSelect');
$this->pager->setPage($this->page);
$this->pager->init();
You can reference this link http://www.symfony-project.org/cookbook/1_0/en/pager for more information .
I hope that answers your question;
There is an error in your query:
$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->add(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
Resulting query: rc_profile.age >= :age1.
If you are looking for the objects with age between $age1 and $age2, this should do the trick:
$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->addAnd(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
Resulting query: rc_profile.age >= :age1 AND rc_profile.age <= :age2
Note that you should use $criteria->addAnd or $criteria->addOr to combine the conditions FOR THE SAME COLUMN.

Categories