Taking existing php search and reconstucting to OOP - php

Happy New Year Everyone!
I have this existing php code I did when I first started and I was wondering if there a way to make it OOP or if I should find another way of recreating it. All opinions welcome.
<?php
require("local_php.inc");
DatabaseConnect();
//Variables to post to results.php
$keyword=$_POST['keyword'];
$location=$_POST['location'];
$type=$_POST['type'];
//Query
$q1 = "SELECT ssearch.id, ssearch.Company, ssearch.Address, ssearch_state.state_location, ssearch.City, ssearch.Zip, ssearch.Phone, ssearch.Description, ssearch.Email,
ssearch_event.Event_name FROM
(ssearch LEFT OUTER JOIN ssearch_state ON ssearch.State = ssearch_state.id)
LEFT OUTER JOIN ssearch_event ON ssearch.Event_id = ssearch_event.id
WHERE ssearch.Company LIKE '%$keyword%'";
if($location != "all")
{
$q1 .= "AND ssearch_state.state_location ='$location' ";
}
if($type != "all")
{
$q1 .= "AND ssearch_event.Event_name ='$type' ";
}
$q1 .= "ORDER BY ssearch.Company DESC LIMIT 0, 3;";
$result = mysql_query($q1);
$q1_total_rows = mysql_num_rows($result);
?>
<div id="content">
<?php if($q1_total_rows >= 1) { ?>
<?php while ($record = mysql_fetch_assoc($result)) { ?>
<div id="table">
<table width="379" height="64" border="0" align="left" bordercolor="#FFFF00" bgcolor="#FEFFD5">
<tr>
<td width="187"><p align="center" class="contentfontsmallsearch"><?php echo $record["Company"];?></p></td>
<td width="182"><p align="center" class="contentfontsmallsearch"><?php echo $record["Event_name"];?></p></td>
</tr>
<tr>
<td><p align="center" class="contentfontsmallsearch"><?php echo $record["state_location"];?></p></td>
<td><p align="center" class="contentfontsmallsearch"><?php echo $record["Description"];?></p></td>
<tr>
</table>
</div>
<?php } ?>
<? } else { ?>
<div id="table2">
<h3 align="center" class="noresults">No Results</h3><br />
<p align="center" class="contentfontsmallsearch">Please Try your Search Again</p><br />
<form action="results.php" method="post" name="myform" id="myform">
<table width="379" height="190" border="0" align="left" bordercolor="#FFFF00" bgcolor="#FEFFD5">
<tr>
<td width="143"><p align="center" class="contentfontsmallsearch">Keyword</p></td>
<td width="226">
<label>
<input name="keyword" type="text" id="keyword" size="30" />
</label>
</td>
</tr>
<tr>
<td><p align="center" class="contentfontsmallsearch">Location</p></td>
<td>
<select name="location">
<option value="all" selected="selected">All Locations</option>
<option value="Pennsylvania">Pennsylvania</option>
<option value="New Jersey">New Jersey</option>
<option value="Delaware">Delaware</option>
</select>
</td>
<tr>
<td width="143"><p align="center" class="contentfontsmallsearch">Event Type </p></td>
<td>
<select name="type">
<option value="all" selected="selected">All Events</option>
<option value="Rally Event">Rally Event</option>
<option value="Awareness Event">Awareness Event</option>
<option value="Donation Event">Donation Event</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit2" value="Search" /></td>
</tr>
</table>
</form>
</div>
<? } ?>
</div>

I would prefer if you first made it secure. Add mysql_real_escape_string when reading the $_POST input variables. As it stands now, your code relies on magic_quotes to work at all.
As second step, you could make this code procedural. Devise a location_query() function or something. I see no immediate structure that would benefit to jump to "making it OOP".

Related

MSSQL procedure query return "Cannot insert the value NULL into column"

I have a form of 5 fields. when click on submit button it will fetch Amount against the fields from database MSSQL. Now when i fill form and submit it is giving an error. i have used MSSQL procedure query to execute it. please help me out i am in trouble now.
<div id="middle_pane">
<div>
<div id="inner_pane">
<div id="center_pane">
<div id="center_inner_pane">
<div class="txt_content">
<table width="385" height="200" style=" border:2px solid silver"
cellpadding="6px" cellspacing="0px" align="center" border="0">
<form method="POST" action="<?php bloginfo('url');?>/fedex-tariff">
<tr>
<td colspan="2" style="background:#0066FF; color:#FFFFFF;
fontsize:50px">Fedex Rate Card</td></tr>
<tr>
<td>Origin</td>
<td>
<select name="origin" id="origin" onChange="">
<option value="pakistan">Pakistan</option>
</select>
</td>
</tr>
<tr>
<td>Select Origin city </td>
<td>
<select name="city_id" id="city_id">
<option value="">--SELECT CITY--</option>
<?php
$city = "select * from branches";
$city_query = mssql_query($city);
while ($row_city = mssql_fetch_array($city_query)){
?>
<option value="<?php echo $row_city['name'];?>"> <?php echo
$row_city['name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td>Select Destination </td>
<td>
<select name="country_id" id="country_id">
<option value="">--SELECT Destination--</option>
<?php
$service= "select * from country";
$service_query= mssql_query($service);
while ($row_service = mssql_fetch_array($service_query)){
?>
<option value="<?php echo $row_service['Name'];?>"> <?php
echo $row_service['Name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td> Selection of Weight </td>
<td>
<div id="wgt">
<input type="text" id="weight" name="weight" value=""
placeholder="weight...." required="">
</div>
</td>
</tr>
<tr>
<td>Select Your Service</td>
<td>
<select name="service_id" id="service_id">
<option value="">--SELECT--</option>
<?php
$service_type = "SELECT distinct ServiceTypeId from
dbo.IntlZoneCountry where ServiceTypeId IN ('FedEx Cash Tariff
Docs','FedEx Box','FedEx Student Package')";
$res_type = mssql_query($service_type);
$cnt_type = mssql_num_rows($res_type);
while($row_type = mssql_fetch_array($res_type)){
?>
<option value="<?php echo $row_type['ServiceTypeId'];?>"><?php
echo $row_type['ServiceTypeId'];?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td> <input type="submit" value="Calculate" name="submit"
class="btn"></td>
</tr>
</form>
<table width="385" style=" border:2px solid silver"
cellpadding="6px" cellspacing="0px" align="center" border="0">
<tr>
<td>
<?php
if(isset($_POST['submit']))
{
$city_id = $_POST['city_id'];
$country_id = $_POST['country_id'];
$service_id = $_POST['service_id'];
$weight_id = $_POST['weight'];
if(empty($city_id) || empty($country_id) || empty($service_id) ||
empty($weight_id)){
?><center><?php echo "Please filled all the fields first.";?>
</center>
<?php
}
else{
$s = "[SP_UpdateCNPrice_Int_Tariff]
'$weight_id','$city_id','$country_id','$service_id'";
$result = mssql_query($s);
while($row = mssql_fetch_assoc($result)){
?>
</td>
</tr>
<tr style="background:#00CFFF; color:blue;">
<td>
<center>
<font size="3">
<?php
$val = $row['amount'];
$gst = $row['gst'];
echo "<strong>"."Rate : "."$".""." ".$val.$gst;
?>
</font>
</center>
</td>
</tr>
<tr style="background:#00CFFF; color:blue;">
<td><center> All Rates are Exclusive of 16% GST + 23% fuel
Charges (FSC).</center> </td>
</tr>
<?php
}
}
}
?>
</table>
</div>
</div>
</div>
</div>
</div>
MSSQL Procedure Screenshot
Front Display of Website

PHP Unable to POST select box values

I'am trying to get some values from users and after that I'am inserting these values to my database but I can't get some of my select boxes's values using $_POST.
Users first select intership type via selectbox and depending on value of this selectbox the bottom menu changes via jQuery (That's why these IENG349ReqCourses and IENG449ReqCourses div's are hidden).
But I can't get the values of grade selecboxes inside of these hidden div's. Rest of input fields are okay, I can get their values and save to my database.
HTML:
<form id="intershipStage1Form" method="POST" action="form1.php">
<div id="generalInfo">
<center>
<table>
<tr>
<td colspan="2" valign="middle" align="center">
<label id="stuNameLabel">Student Full Name: <?php echo $_SESSION['username']; ?></label>
</td>
</tr>
<tr>
<td title="Your student number contains only numbers and should be 11 digits " valign="middle"><label id="stuNumberLabel">Student Number:</label></td>
<td><input id="stuNumberText" class="form1Text" type="text" name="stuNumberText" /></td>
</tr>
<tr>
<td title="A sample format: 3.16 and do not use comma(,)" valign="middle"><label id="stuGPALabel">Student GPA:</label></td>
<td><input id="stuGPAText" class="form1Text" type="text" name="stuGPAText" /></td>
</tr>
<tr>
<td title="Choose your academic advisor" valign="middle"><label id="stuAdvisorLabel">Student Advisor:</label></td>
<td align="center">
<select id="advisorSelectionBox" name="advisorSelectionBox">
<option value="">--select--</option>
<?php
$userType = "A";
$stmt = $db->prepare("SELECT * FROM users WHERE UserType = ?");
if($stmt == "false"){
die('Query error !'.$db->error);
}
$stmt->bind_param('s', $userType);
$stmt->execute();
$result = $stmt -> get_result();
while($advisor = $result ->fetch_array(MYSQLI_BOTH)){
echo '<option value="'.$advisor["UserID"].'">'.$advisor['FirstName']." ".$advisor['LastName'].'</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td title="Choose your current study year" valign="middle">
<label>Study Year:</label>
</td>
<td align="center">
<select id="studyYearBox" name="studyYearBox">
<option value="">--select--</option>
<option value="SOPHOMORE">SOPHOMORE</option>
<option value="JUNIOR">JUNIOR</option>
<option value="SENIOR">SENIOR</option>
</select>
</td>
</tr>
<tr>
<td title="Choose your internship type before try to sent this form to your advisor" valign="middle">
<label>Internship Type:</label>
</td>
<td align="center">
<select id="intershipTypeBox" name="intershipTypeBox">
<option value="">--select--</option>
<option value="IENG349">IENG 349</option>
<option value="IENG449">IENG 449</option>
</select>
</td>
</tr>
</table>
</center>
</div>
<div id="litleSpacer"></div>
<div id="IENG349ReqCourses" class="reqCourses" hidden="true">
<center>
<table>
<caption style="color:#f00;">Please select your grades</caption>
<tr>
<td valign="middle">
<label>IENG 102 - Intro to IE:</label>
</td>
<td>
<select name="IENG102Grade">
<option value="">--select--</option>
<option value="A">A</option>
<option value="A-">A-</option>
<option value="B+">B+</option>
<option value="B">B</option>
<option value="B-">B-</option>
</select>
</td>
</tr>
</table>
</center>
</div>
<div id="IENG449ReqCourses" class="reqCourses" hidden="true">
<center>
<table>
<caption style="color:#f00;">Please select your grades</caption>
<tr>
<td valign="middle">
<label>IENG 312 - System Simulation:</label>
</td>
<td>
<select name="IENG312Grade">
<option value="">--select--</option>
<option value="A">A</option>
<option value="A-">A-</option>
<option value="B+">B+</option>
<option value="B">B</option>
<option value="B-">B-</option>
</select>
</td>
</tr>
</table>
</center>
</div>
<div id="litleSpacer"></div>
<input id="sendButton" type="submit" name="sendButton" value="SEND FOR APPROVEMENT"/>
</form>
PHP:
<?php
$stuID = $_SESSION['user_id'];
$stuFullName = $_SESSION['username'];
$stuNumber = $_POST['stuNumberText'];
$stuGPA = $_POST['stuGPAText'];
$stuAdvisor = $_POST['advisorSelectionBox'];
$studyYear = $_POST['studyYearBox'];
$internshipType = $_POST['intershipTypeBox'];
$coordinatorAppr = "SENT";
$advisorAppr = "SENT";
$IENG102 = $_POST['IENG102Grade'];
$IENG312 = $_POST['IENG312Grade'];
$insert_stmt = $db->prepare("INSERT INTO internship_form_info VALUES(NULL,?,?,?,?,?,?,?,?,?)");
if($insert_stmt === false){
die('Query Error: '.$db->error);
}
$insert_stmt ->bind_param("sssssssii",$stuFullName,$stuNumber,$stuGPA,$studyYear,$internshipType,$advisorAppr,$coordinatorAppr,$stuID,$stuAdvisor);
$insert_stmt ->execute();
$formID = $db->insert_id;
if($internshipType=="IENG349"){
$insert_stmt2 = $db->prepare("INSERT INTO ieng349_req_courses_grades VALUES(NULL,?,?,?)");
if($insert_stmt2 === false){
die('Query Error: '.$db->error);
}
$insert_stmt2 ->bind_param("iisssssss",$stuID,$formID,$IENG102);
$insert_stmt2 ->execute();
}
else if($internshipType=="IENG449"){
$insert_stmt3 = $db->prepare("INSERT INTO ieng449_req_courses_grades VALUES(NULL,?,?,?)");
if($insert_stmt3 === false){
die('Query Error: '.$db->error);
}
$insert_stmt3 ->bind_param("iisssssssssssss",$stuID,$formID,$IENG312);
$insert_stmt3 ->execute();
}
$db->close();
So what can be the problem? Any help would be appriciated!
You have to make one option as selected to get it posted to the server.
If field is hidden and no option is marked selected, you will get no value on server.
I suggest to you, to check for example in Live Http Headers, which $_POST date you send. Also in your very top of script put
var_dump($_POST);

One input not posting PHP/MSSQL

Trying to use this page to update an MSSQL table. My hidden field named hdnLine (2nd to last line of code) is not being posted on submit, though it is being populated with a value prior to the form being submitted. I had the page working, and all of the sudden I keep getting "Notice: Undefined index: hdnLine in C:\inetpub\wwwroot\clientadmin\agent-edit.php on line 11" when I try to submit. I have been messing with it all day trying to debug etc. and have had no luck with it. I'm assuming its a syntax error of sorts but can't figure it out. Any help would be gladly appreciated. The code is all below minus a few includes for db connectivity. I will do my best to help answer any questions pertaining.
<?php
if($_GET["Action"]=="Save"){
for($i=1;$i<=$_POST["hdnLine"];$i++)
{
$strSQL = "UPDATE RENTAGNT SET ";
$strSQL .="SPIFF = '".$_POST["txtSPIFF$i"]."' ";
$strSQL .=",PROMO = '".$_POST["txtPROMO$i"]."' ";
$strSQL .=",SPIFFVAL = '".$_POST["txtSPIFFVAL$i"]."' ";
$strSQL .=",AGNTLVL = '".$_POST["txtAGNTLVL$i"]."' ";
$strSQL .=",SPIFFDAYS = '".$_POST["txtSPIFFDAYS$i"]."' ";
$strSQL .=",MASTAGNTID = '".$_POST["txtMASTAGNTID$i"]."' ";
$strSQL .="WHERE RENTAGNTID = '".$_POST["hdnRENTAGNTID$i"]."' ";
$stmt = $conn->query($strSQL);
}
//exit();
}
$strSQL="SELECT RENTAGNTID, RENTAGNT.AGENTCODE, NAME, AGNTLVL,SPIFF, SPIFFVAL, PROMO, SPIFFDAYS,INACTIVE, MASTAGNTID FROM RENTAGNT WHERE INACTIVE='0' GROUP BY MASTAGNTID, RENTAGNT.AGENTCODE, RENTAGNT.NAME, RENTAGNTID, SPIFF, SPIFFVAL, SPIFFDAYS, AGNTLVL, INACTIVE, PROMO";
$stmt = $conn->query($strSQL);
?>
<form name="frmMain" method="POST" action="?Action=Save" >
<table id="res-list-table" class="tablesorter" width="90%" border="1" background="#fff">
<thead> <tr>
<th width="10%"> <div align="center">Rental Agent ID</div></th>
<th width="20%"> <div align="center">Agent Code </div></th>
<th width="35%"> <div align="center">Name </div></th>
<th width="5%"> <div align="center">1 Free</div></th>
<th width="5%"> <div align="center">Spiff Yes/No</div></th>
<th width="5%"> <div align="center">Spiff Amount </div></th>
<th width="5%"> <div align="center">Days Req. <br/> for Spiff</div></th>
<th width="5%"> <div align="center">Master Agent</div></th>
<th width="5%"> <div align="center">Agent Level</div></th>
</tr>
</thead><tbody>
<?php
$i = 1;
while ($row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
$i++;
?>
<tr>
<td><div align="center">
<?php echo $row["RENTAGNTID"]; ?>
</div></td>
<td><div align="center">
<?php echo $row["AGENTCODE"]; ?>
</div></td>
<td><div align="center">
<?php echo $row["NAME"]; ?>
</div></td>
<td>
<input type="hidden" name="hdnRENTAGNTID<?=$i;?>" size="6" value="<?=$row["RENTAGNTID"];?>"/>
<select name="txtPROMO<?=$i;?>">
<option value="0" <?php if($row["PROMO"]=="0") { echo "selected='selected'"; } ?> >No</option>
<option value="1" <?php if($row["PROMO"]=="1") { echo "selected='selected'"; } ?> >Yes</option>
</select></td>
<td>
<select name="txtSPIFF<?=$i;?>">
<option value="0" <?php if($row["SPIFF"]=="0") { echo "selected='selected'"; } ?> >No</option>
<option value="1" <?php if($row["SPIFF"]=="1") { echo "selected='selected'"; }?> >Yes</option>
</select></td>
<td><input type="text" name="txtSPIFFVAL<?=$i;?>" size="5" style="width:75px;" value="<?=$row["SPIFFVAL"];?>" /></td>
<td><input type="text" name="txtSPIFFDAYS<?=$i;?>" size="5" style="width:75px;" value="<?=$row["SPIFFDAYS"];?>" /></td>
<td><input type="text" name="txtMASTAGNTID<?=$i;?>" size="5" style="width:75px;" value="<?=$row["MASTAGNTID"];?>" /></td>
<td>
<select name="txtAGNTLVL<?=$i;?>">
<option value="1" <?php if($row["AGNTLVL"]==1) { echo "selected='selected'"; }?> >Agent</option>
<option value="2" <?php if($row["AGNTLVL"]==2) { echo "selected='selected'"; }?> >Master Agent</option>
<option value="3" <?php if($row["AGNTLVL"]==3) { echo "selected='selected'"; }?> >Admin</option>
</select>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<input type="submit" name="submit" value="submit1" />
<input type="hidden" name="hdnLine" value="<?=$i;?>"/>
</form>
Seems like the short tags used to assign the value for the <input type="hidden" name="hdnLine" value="<?=$i;?>"/>. Is the short_open_tag = On in your php.ini?
If not change to on.
Or
Change throughout the code where <?=?> to <?php=?>

HTML Tables not visible, from PHP code

I am attempting to make part of a webpage, that basically shows what groups a user has joined. I want to put one table for the form 'makegroup' and another table for the 'groups owned', both these tables are part of the row of another bigger table. But however much I try the 'makegroup' is not getting displayed, and 'groupsowned' is taking its place completely. Any idea why this is happening? Should I post code?
<table width="90%" height="125" >
<tr bgcolor="#CCCCCC"><td height="20" colspan="2"><h4>Groups</h4>
</td></tr>
<tr bgcolor="#CCCCCC">
<?
if($accountid == $_SESSION['userid'])
{
?>
<td width="50%">
<form name="groupscreate" method="post" onsubmit="return Confirm()">
<table width="99%" height="6%" bgcolor="#CCCCCC"> <tr valign="top">
<td colspan="3"><h5>Create Groups</h5></td></tr>
<tr><td>
<input align="absmiddle" type="text" maxlength="25" name="gname" /><? if($nog==1) {echo "<br /><font color=red>Please enter a valid groupname</font>";}?></td>
<td><select name="gcategory">
<option value="General" selected>General</option>
<option value="Comedy">Comedy</option>
<option value="Education">Education</option>
<option value="Entertainment">Entertainment</option>
<option value="Gaming">Gaming</option>
<option value="Music">Music</option>
<option value="Science & Technology">Science & Technology</option>
<option value="Sports">Sports</option>
</select></td><td><input type="submit" name="makegroup" value="create group"/></td></tr>
</table>
</form>
</td>
<?
}
$grp= "SELECT * from groups where accountid='". $accountid."'";
$rest = mysql_query($grp,$connection);
$cont=0;
?>
<td width="50%" >
<table width="100%" height="20%" bgcolor="#CCCCCC" >
<tr><td colspan="10"><h5>Groups Owned</h5>
</td></tr>
<?
while($grow=mysql_fetch_array($rest))
{
$cont=$cont+1;
$gid=$grow['groupid'];
$gnam=$grow['groupname'];
$gcreator=$grow['accountid'];
if($cont==0)
{
echo "<tr>";
}
echo "<td align='left' ><a href='groups_discussions.php?id=".$gid."'><font color="."#333333"." size='-1'>".$gnam."</font></a></td>";
if($cont==5)
{echo "</tr>";}
}
?>
</table>
</td>
</tr></table>
I want to know if everything looks all right to you guys. I can;t see anything wrong, but the fact is that the form is not getting displayed :(
You mean like this?
<table width="90%" height="125" bgcolor="orange">
<tr bgcolor="#CCCCCC"><td height="20" colspan="2"><h4>Groups</h4></td></tr>
<tr bgcolor="#CCCCCC">
<td width="50%">
<form name="groupscreate" method="post" onsubmit="return Confirm()">
<table width="99%" height="6%" bgcolor="green">
<tr valign="top">
<td colspan="3"><h5>Create Groups</h5></td>
</tr>
<tr bgcolor="blue">
<td bgcolor="orange">
<input align="absmiddle" type="text" maxlength="25" name="gname" />
<?php
$nog = 1;
if($nog == 1)
echo "<br /><font color=red>Please enter a valid groupname</font>";
?>
</td>
<td bgcolor="yellow">
<select name="gcategory">
<option value="General" selected>General</option>
<option value="Comedy">Comedy</option>
<option value="Education">Education</option>
<option value="Entertainment">Entertainment</option>
<option value="Gaming">Gaming</option>
<option value="Music">Music</option>
<option value="Science & Technology">Science & Technology</option>
<option value="Sports">Sports</option>
</select>
</td>
<td bgcolor="red">
<input type="submit" name="makegroup" value="create group"/>
</td>
</tr>
</table>
</form>
</td>
<td width="50%">
<table width="100%" height="20%" bgcolor="pink" >
<tr><td colspan="10"><h5>Groups Owned</h5></td></tr>
<?php
//$grp= "SELECT * from groups where accountid='". $accountid."'";
//$rest = mysql_query($grp, $connection);
$rest = array(
array('groupid' => '123', 'groupname' => 'testname', 'accountid' => '456'),
array('groupid' => '678', 'groupname' => 'testname2', 'accountid' => '999')
);
$cont = 0;
foreach ($rest as $grow)
{
$gid = $grow['groupid'];
$gnam = $grow['groupname'];
$gcreator = $grow['accountid'];
if($cont == 0)
echo "<tr>";
echo "<td align='left' ><a href='groups_discussions.php?id=" . $gid . "'><font color="."#333333"." size='-1'>" . $gnam . "</font></a></td>";
if($cont == 1)
echo "</tr>";
$cont = $cont + 1;
}
?>
</table>
</td>
</tr>
</table>
You had a pile of problems in your code, such as only being able to handle a single set of 5 results in the second table, incrementing a counter (so it's minimum value would always be 1) and then testing to see if it was zero, etc.
<table width="90%" height="125" >
<tr bgcolor="#CCCCCC">
<td height="20" colspan="2">
<h4>Groups</h4>
</td>
</tr>
<tr bgcolor="#CCCCCC">
<?
if( isset( $_SESSION['userid'] ) && $accountid==$_SESSION['userid'] ){
?>
<td width="50%">
<form name="groupscreate" method="post" onsubmit="return Confirm()">
<table width="99%" height="6%" bgcolor="#CCCCCC">
<tr valign="top">
<td colspan="3">
<h5>Create Groups</h5>
</td>
</tr>
<tr>
<td>
<input align="absmiddle" type="text" maxlength="25" name="gname" />
<?
if( $nog==1 ){
?>
<br /><font color="red">Please enter a valid groupname</font>
<?php
}
?>
</td>
<td>
<select name="gcategory">
<option value="General" selected>General</option>
<option value="Comedy">Comedy</option>
<option value="Education">Education</option>
<option value="Entertainment">Entertainment</option>
<option value="Gaming">Gaming</option>
<option value="Music">Music</option>
<option value="Science & Technology">Science & Technology</option>
<option value="Sports">Sports</option>
</select>
</td>
<td>
<input type="submit" name="makegroup" value="create group" />
</td>
</tr>
</table>
</form>
</td>
<?
}
$grp = "SELECT * from groups where accountid='{$accountid}'";
$rest = mysql_query( $grp , $connection );
$cont = 0;
?>
<td width="50%" >
<table width="100%" height="20%" bgcolor="#CCCCCC" >
<tr>
<td colspan="10">
<h5>Groups Owned</h5>
</td>
</tr>
<?
if( mysql_num_rows( $rest )>0 ){
while( $grow = mysql_fetch_array( $rest ) ){
$gid = $grow['groupid'];
$gnam = $grow['groupname'];
$gcreator = $grow['accountid'];
if( ( $cont%5 )==0 )
echo '<tr>';
echo "<td align='left' ><a href='groups_discussions.php?id={$gid}'><font color='#333333' size='-1'>{$gnam}</font></a></td>\n";
if( ( $cont%5 )==4 )
echo '</tr>';
$cont++;
}
}else{
echo '<tr><td colspan="10">No Records</td></tr>';
}
?>
</table>
</td>
</tr>
</table>
Might help.
Check whether this condition is matching or not: if($accountid == $_SESSION['userid'])
If it doesn't matches, then the form for makegroup won't be visible.
I have tidied up your code a bit for better readability:
<table width="90%" height="125" >
<tr bgcolor="#CCCCCC">
<td height="20" colspan="2"><h4>Groups</h4></td>
</tr>
<tr bgcolor="#CCCCCC">
<?
if($accountid == $_SESSION['userid'])
{
?>
<td width="50%">
<form name="groupscreate" method="post" onsubmit="return Confirm()">
<table width="99%" height="6%" bgcolor="#CCCCCC">
<tr valign="top">
<td colspan="3"><h5>Create Groups</h5></td>
</tr>
<tr>
<td>
<input align="absmiddle" type="text" maxlength="25" name="gname" />
<? if($nog==1) {echo "<br /><font color=red>Please enter a valid groupname</font>";}?>
</td>
<td>
<select name="gcategory">
<option value="General" selected>General</option>
<option value="Comedy">Comedy</option>
<option value="Education">Education</option>
<option value="Entertainment">Entertainment</option>
<option value="Gaming">Gaming</option>
<option value="Music">Music</option>
<option value="Science & Technology">Science & Technology</option>
<option value="Sports">Sports</option>
</select>
</td>
<td>
<input type="submit" name="makegroup" value="create group"/>
</td>
</tr>
</table>
</form>
</td>
<?
}
$grp= "SELECT * from groups where accountid='". $accountid."'";
$rest = mysql_query($grp,$connection);
$cont=0;
?>
<td width="50%" >
<table width="100%" height="20%" bgcolor="#CCCCCC" >
<tr>
<td colspan="10"><h5>Groups Owned</h5></td>
</tr>
<?
while($grow=mysql_fetch_array($rest))
{
$cont=$cont+1;
$gid=$grow['groupid'];
$gnam=$grow['groupname'];
$gcreator=$grow['accountid'];
if($cont==0)
echo "<tr>";
echo "<td align='left' ><a href='groups_discussions.php?id=".$gid."'><font color="."#333333"." size='-1'>".$gnam."</font></a></td>";
if($cont==5)
echo "</tr>";
}
?>
</table>
</td>
</tr>
</table>
Few things:
1) This part...
$cont=$cont+1;
$gid=$grow['groupid'];
$gnam=$grow['groupname'];
$gcreator=$grow['accountid'];
if($cont==0)
{
echo "<tr>";
}
...will never fire the if statement since on the first iteration $cont=1. ($cont=0+1=1)
2) You need to reset $cont after you close your if($cont==5){echo "</tr>";}} so the top section of the while statement creates a new <tr> tag.

Check drop down and create if statement based on that selection

I want to create a php variable depending on what was selected in a drop down.
This is the drop down:
<td width="5" rowspan="2"><select id="ddlTime">
<option selected="selected" value="perhour" name="perhour">per hour</option>
<option value="perannum" name="perannum">per annum</option>
</select></td>
Depending if "per hour" or "per annum" was selected, I want to do the following (I'm not quite sure syntax wise if this is correct and this part is on another page):
// if per hour is selected:
$result_pharmacist = $_POST["pharmacist"];
$result_dispenser = $_POST["dispenser"];
// if per annum is selected:
$user_pharmacist = $_POST["pharmacist"];
$result_pharmacist = $user_pharmacist/37.5/52;
$user_dispenser = $_POST["dispenser"];
$result_dispenser = $user_dispenser/37.5/52;
How can this be done?
Here's my full form:
<form action="<?php the_permalink(); ?>calculations" method="post">
<h2>Savings calculator</h2>
<div class="calculator-divide"></div>
<table border="0">
<tr>
<td colspan="3"><h3>Current service costs</h3></td>
</tr>
<tr>
<td width="440"><p>Pharmacist</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input style="width:145px!IMPORTANT;" value="22.00" type="text" name="pharmacist" />
</p></td>
<td width="5" rowspan="2"><select id="ddlTime">
<option selected="selected" value="perhour" name="perhour">per hour</option>
<option value="perannum" name="perannum">per annum</option>
</select></td>
</tr>
<tr>
<td><p>Dispenser / Technician</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input style="width:145px!IMPORTANT;" value="8.00" type="text" name="dispenser" />
</p></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3"><h3>Time taken to carry out manual dispensing tasks</h3></td>
</tr>
<tr>
<td><p>Measure 50mls dose by hand including Pharmacist check</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="1" type="text" name="measure-check" />
Minute(s)</p></td>
</tr>
<tr>
<td><p>Preparing labels from dispensary system</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="0.5" type="text" name="labels" />
Minute(s)</p></td>
</tr>
<tr>
<td><p>Write up CD register</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="2" type="text" name="cd-register" />
Minute(s)</p></td>
</tr>
<tr>
<td></td>
<td colspan="3"><div class="estimate">
<input style="margin-bottom:20px;" type="submit" value="Estimate my savings" />
</div></td>
</tr>
</table>
</form>
<select id="ddlTime" name="ddlTime">
and
if( $_POST['ddlTime']=='perhour' ){
// if per hour is selected:
$result_pharmacist = $_POST["pharmacist"];
$result_dispenser = $_POST["dispenser"];
}elseif( $_POST['ddlTime']=='perannum' ){
// if per annum is selected:
$user_pharmacist = $_POST["pharmacist"];
$result_pharmacist = $user_pharmacist/37.5/52;
$user_dispenser = $_POST["dispenser"];
$result_dispenser = $user_dispenser/37.5/52;
}
I hope this help you :
<?php
if(isset ($_POST['save']))
{
$temp = $_POST['opsi'];
echo $temp;
}
?>
<html>
<body>
<form action="newEmptyPHP.php" method="POST">
<h3> Choose Your Option</h3>
<select name="opsi">
<option value=0 selected>- Customer -</option>
<option value="perannum" name="perannum">perannum</option>
<option value="perhour" name="perhour">perhour</option>
</select> <br> <br>
<input type="submit" name="save" value="Save and Commit">
</form>
</body>
</html>

Categories