PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 27th, 2013, 02:02 PM
eropsy eropsy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 51 eropsy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 11 m 10 sec
Reputation Power: 5
Elseif in drop down select form

Hi, the form below drops down a list of users.

I would like to add an elseif statement where it shows only one option value.
That is to say, if (isset($_SESSION['login_rights']) and $_SESSION['login_rights'] < 3) then show only select option where field= '".$field."'" .

Where would I plug in the values in the form to make it work?

Any help would be greatly appreciated. REALLY!
Thanks!

PHP Code:
<form method="post">
<
select name="section" size="1" onchange="this.form.submit();"
  
<
option value="nothing">Select</option>

<?
php $Users=mysql_query("Select * from " .$table_customer" where rights=2 and groupfield= '".$groupfield."'");

if(isset(
$Users)) { if(mysql_num_rows($Users)>0){ while($us=mysql_fetch_array ($Users)){
$sel="";
if (
$us['id']==$customer_id)
$sel='selected="selected"';
echo 
'<option value="' $us['id'] .'"' .$sel .' >'.$us['first_name']." ".$us['last_name']."</option>";
}}}

?>
</select>
</form> 

Reply With Quote
  #2  
Old February 27th, 2013, 02:14 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,879 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 1 Day 19 h 33 m 20 sec
Reputation Power: 581
1) I see some very poor programming practices that makes your code very difficult to read. You need to format your code with proper blocks and use indentation. Based on what you posted it is not clear what you are really asking and your hard-to-read code does not help.
2) You should not be using the deprecated MySQL extensions. While you are reformatting your code, switch to PDO.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #3  
Old February 27th, 2013, 02:15 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,805 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 17 h 43 m 13 sec
Reputation Power: 6112
PHP Code:
<?php 

$sql 
"Select * from " .$table_customer" where rights=2 and groupfield= '".$groupfield."'";

if (isset(
$_SESSION['login_rights']) && $_SESSION['login_rights'] < 3) {
  
$sql .= " AND someField = '" $someValue "'";
}

$Users=mysql_query($sql);
You should be escaping any user-submitted or variable values with mysql_real_escape_string.

You should also be using mysqli or PDO.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Last edited by ManiacDan : February 27th, 2013 at 02:49 PM.

Reply With Quote
  #4  
Old February 27th, 2013, 02:36 PM
eropsy eropsy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 51 eropsy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 11 m 10 sec
Reputation Power: 5
thanks for your reply GW1500se and ManiacDan.

I'm going to redo the codes with PDO...and restructured see if it works...

The code was written by some guy I hired a year ago-before I knew anything about PHP. It works so I thought nothing of it. But I'm learning PHP now and am just studying the codes I have before me....

Reply With Quote
  #5  
Old February 27th, 2013, 03:26 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,869 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 57 m 55 sec
Reputation Power: 813
Quote:
Originally Posted by eropsy
The code was written by some guy I hired a year ago-before I knew anything about PHP. It works so I thought nothing of it.


Well, hopefully you've learnt from that experience.

Whether or not code "works" tells you nothing about its quality, especially when "verifying" it merely consist of checking the output.

Pretty much anybody can throw some HTML together for a couple of bucks. Problem is, this is only the easy part of the job. The hard part consists of structuring the code properly, using functionalities in an intelligent way, making the code readable, taking care of security and error handling etc. And that's what those One dollar programmers usually don't get right.

So don't stop at "it works". Should you ever want to hire somebody again, let him/her give you some example code first and have it checked by people who know their stuff (friends, online communities etc.). In this particular case, you'd have found out very soon that this guy knows nothing about security or modern PHP, let alone good code.

This suggestion also applies to your own code. "It works" is not enough, so don't be satisfied just because your code happens to output the right data.

Reply With Quote
  #6  
Old February 27th, 2013, 03:49 PM
eropsy eropsy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 51 eropsy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 11 m 10 sec
Reputation Power: 5
Yup, I'm just finding that out.

Well, I cleaned up the code that the guy wrote for me. It was messy with alot of unnecessary stuff in it. I changed it to this.


PHP Code:
<form method="post">

<
select name="section" size="1" onchange="this.form.submit();">   
<
option value="nothing">Select</option>
<?
php

$result 
mysql_query("Select * from " .$table_customer" where rights=2 and groupfield= '".$groupfield."'");
while(
$data=mysql_fetch_array ($result)){
$sel="";
if (
$data['id']==$customer_id)
$sel='selected="selected"';
echo 
'<option value="' $data['id'] .'"' .$sel .' >'.$data['company']." </option>";
}

?>
</select>
</form> 



I'm not going to change it to any of the new APIs because this little scrap of code is embedded in a bigger program using the old MySql....

It isn't a new development...

I still have to figure out how to add the elseif in there.

Last edited by eropsy : February 27th, 2013 at 04:47 PM.

Reply With Quote
  #7  
Old February 27th, 2013, 05:57 PM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 295 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 37 m 29 sec
Reputation Power: 5
Well, are you truely aiming for an elseif, or would an if/else suffice? Say a liquor store checks age to provide a drop-down of liquors... Their drop-down options could be similar to:
PHP Code:
if($age >= 21) { ?>
  <option value="vodka">Vodka Name</option>
  <option value="rum">Rum Name</option>
  <option value="whiskey">Whiskey Name</option>
<?php } else { ?>
  <option value="under21">I'm sorry. You are not of legal age.</option>
<?php 

Otherwise, if you do wish for the elseif...
PHP Code:
// Simply change:
} else {
// into
} elseif($age 21) { 


'else' will ALWAYS run if the prior if() fails. elseif() will ONLY run if the prior if() fails, AND the statement within is TRUE.

And a little suggestion... Try your best to hold items, no matter how long/short, within tags, such as...
PHP Code:
// Your statement
if ($data['id']==$customer_id)
$sel='selected="selected"';

// Suggested.
if ($data['id']==$customer_id) {
  
$sel='selected="selected"';
}

// Or even, if single/short line...
if ($data['id']==$customer_id) { $sel='selected="selected"'; } 

Last edited by Triple_Nothing : February 27th, 2013 at 07:47 PM.

Reply With Quote
  #8  
Old February 27th, 2013, 06:44 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,869 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 57 m 55 sec
Reputation Power: 813
Quote:
Originally Posted by eropsy
I'm not going to change it to any of the new APIs because this little scrap of code is embedded in a bigger program using the old MySql....


That does not release you from securing your data. Both your database values and your HTML values must be escaped. See this thread for further explanations.

Obviously you aren't aware of it, but letting your visitors change your database queries and your HTML pages can have serious consequences, especially since this seems to be some kind of online shop. Losing the customer data and payment info to some frauds is not funny.

So you should take this seriously. Be glad that nothing has happened yet, but don't rely on it.

Reply With Quote
  #9  
Old February 27th, 2013, 07:12 PM
eropsy eropsy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 51 eropsy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 11 m 10 sec
Reputation Power: 5
ThanksTripleNothing for you help. Still working on it.

Thanks Jacques for your feedback. No worries about security issues there.
I'm just in autodidactic school right now. I take issues of securities and privacy very seriously. I don't plan on compromising my personal data or anyone else who's entrusted their personal data to me were I ever to do any business online one day.

BTW, I already visited your link a few days ago on
The 6 worst sins of security , and
http://php.net/manual/en/mysqlinfo.api.choosing.php

Can't learn all that you guys know and have experience in for years in a couple of days....taking it one little scrap code at a time...this one => the drop down select options....

It's pretty amazing all this stuff - and thanks to you all for your help. Really!

Last edited by eropsy : February 27th, 2013 at 07:34 PM.

Reply With Quote
  #10  
Old February 27th, 2013, 07:58 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,869 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 57 m 55 sec
Reputation Power: 813
Quote:
Originally Posted by eropsy
Can't learn all that you guys know and have experience in for years in a couple of days....


Sure.

And rewriting bad code does take a lot of time. Just wanted to make sure you know the issues (I didn't realize we had already talked about that in previous threads).

Reply With Quote
  #11  
Old February 27th, 2013, 08:22 PM
eropsy eropsy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 51 eropsy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 11 m 10 sec
Reputation Power: 5
Yeah you've been real helpful Jaques.

Anyhooo, I learnt something new today.

the variable variables $$

I needed it to do what I wanted to do - that is cordoning off the drop-down select values to specific users...So the code below works now with the $$ added.

there's still a little glitch. Will to get at it tomorrow.

Cheers!

PHP Code:
<form method="post">

<
select name="section" size="1" onchange="this.form.submit();">   
<?
php

$result 
mysql_query("Select * from " .$table_customer" where rights=2 and groupfield= '".$groupfield."'");

$
$customer_id   "selected";

while(
$data=mysql_fetch_array ($result)){

if (
$data['id']==$customer_id
   {
$sel='selected="selected"';
   }  else {
   
$sel '';
          }
 if (
$_SESSION['login_rights'] >= ) {
echo 
'<option value="' $data['id'] .'"' .$sel .' >'.$data['company']." </option>";
}} 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Elseif in drop down select form

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap