Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1683 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Need help working out something

Thread title: Need help working out something
Closed Thread  
Page 1 of 2 1 2 >
    Thread tools Search this thread Display Modes  
12-31-2006, 06:39 PM
#1
Jeff Andersen is offline Jeff Andersen
Status: Superstar
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 4,449
iTrader: 10 / 100%
 

Jeff Andersen is on a distinguished road

  Old  Need help working out something

I have a series of drop down menus such as:

Menu One:
  • Select one please...
  • Basketball
  • Soccer
  • Golf

Menu Two:
  • Select one please...
  • Basketball
  • Soccer
  • Golf

Menu Three:
  • Select one please...
  • Basketball
  • Soccer
  • Golf

And each of the three list items corresponds to a value (For examples say, 1pt, 2pt, 3pt respectively).

Now I've been trying to wrap my head around on the best way to have it so that if a person selects Soccer and Golf, it would then tally the total and submit it to the database. (Total being 5pts)
It also has to recognize when a field contains 'Select one please...' and count that as 0pts.

I've been trying to just do it with simple math functions in PHP, calling each sport's value when called then trying to add them but it gets mucked up when I have any of the 0pt selections.

Any suggestions on the best route to make such a tool?

12-31-2006, 07:07 PM
#2
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

Are your values "2pts" or is it "2"?

If it is the latter,
PHP Code:
$menu1 $_POST['menu1'];
$menu2 $_POST['menu2'];
$menu3 $_POST['menu3'];

$total $menu1 $menu2 $menu3;
if(
$total == '1'// I'm actually not sure if that should be in quotes or not
{
echo 
$total."pt";
} else {
echo 
$total."pts";

HTML Code:
<select name="menu1">
<option value="0">Select One Please...</option>
<option value="1">Basketball</option>
<option value="2">Soccer</option>
<option value="3">Golf</option>
</select>
<!-- And so on... -->
If you need any more help, feel free to add me on MSN.

12-31-2006, 07:22 PM
#3
Xi0s is offline Xi0s
Status: Sin Binner
Join date: Dec 2006
Location: Huddersfield, UK
Expertise:
Software:
 
Posts: 384
iTrader: 3 / 83%
 

Xi0s is on a distinguished road

Send a message via MSN to Xi0s

  Old

Ah well ive just recently done something like this. Go to www.dynamicdrive.net.

http://dynamicdrive.com/dynamicindex...ects/index.htm

I think thats what your looking for.

12-31-2006, 07:36 PM
#4
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

Originally Posted by Xi0s
Ah well ive just recently done something like this. Go to www.dynamicdrive.net.

http://dynamicdrive.com/dynamicindex...ects/index.htm

I think thats what your looking for.
Well, the way I posted and how he described doesn't need that fancy shmancy script.

12-31-2006, 07:45 PM
#5
Jeff Andersen is offline Jeff Andersen
Status: Superstar
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 4,449
iTrader: 10 / 100%
 

Jeff Andersen is on a distinguished road

  Old

It works good, now I just have to work in a verification script and for some reason the statement:

$selecteditem = $_POST['menu1'];
if (isset($_POST['menu1']) !== 0){
print $selecteditem;
}

Isn't working...I've tried putting 0 in a variable, putting all types of quotes around it.

12-31-2006, 07:47 PM
#6
Xi0s is offline Xi0s
Status: Sin Binner
Join date: Dec 2006
Location: Huddersfield, UK
Expertise:
Software:
 
Posts: 384
iTrader: 3 / 83%
 

Xi0s is on a distinguished road

Send a message via MSN to Xi0s

  Old

Ye on reflection, i think andrews solution looks better .

12-31-2006, 07:50 PM
#7
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

The $_GET/POST items will always be strings, so a strict comparison check (!==) will always be false if you're checking against an integer. You can use type casting or the intval function.

12-31-2006, 07:56 PM
#8
Jeff Andersen is offline Jeff Andersen
Status: Superstar
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 4,449
iTrader: 10 / 100%
 

Jeff Andersen is on a distinguished road

  Old

Well here, I'll try to explain a bit more...
My database of 'sports' is setup as so:

ID, Name, Points.

1, Select one..., 0.
2, Golf, 1.
3, Soccer, 2.
4, Basketball, 3.

Now after the form is submitted I want it to show a UL of all the menus that have been selected, but I want the ones that have "Select one..." to be left out. So this is how I setup my menus:


<select name="1">
<?
$result = mysql_query("select * from sports") or die(mysql_error());
while($row = mysql_fetch_array($result)){
print "<option value=\"".$row['id']."\">";
print $row['name'];
print "</option>";
}
?>
</select>
So the value of each menu is the ID from the database, which is where my If statement is coming into play, I want the If statement to rule out anything that has a value of 1 (that being the item in the database containing the name Select one...)

I've tried using < and > and all different operators and just can't figure it out.

12-31-2006, 08:03 PM
#9
Xi0s is offline Xi0s
Status: Sin Binner
Join date: Dec 2006
Location: Huddersfield, UK
Expertise:
Software:
 
Posts: 384
iTrader: 3 / 83%
 

Xi0s is on a distinguished road

Send a message via MSN to Xi0s

  Old

<select name="1">
<?
$result = mysql_query("select * from sports") or die(mysql_error());
while($row = mysql_fetch_array($result)){
print "<option value=\"".$row['points']."\">";
print $row['name'];
print "</option>";
}
?>
</select>


then go for the code selected above.

12-31-2006, 08:04 PM
#10
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

Well, don't you want the value of each option to be the points? If so, change the value of the option to $row['points']. Also, an easier way is to have the Select one... not in the database.. here, give me a minute and I'll edit my post with a snippet.

Also with your code:
PHP Code:
$selecteditem $_POST['menu1'];
if (isset(
$_POST['menu1']) !== 0){
print 
$selecteditem;

The isset() function either returns true or false, so you are basically saying does "true" equal 0 or not.

PHP Code:
ID   Name       Points
----------------------
1    Golf       1
2    Soccer     2
3    Basketball 2


<?php
if(isset($_POST['submit']))
{
    if(isset(
$_POST['1']) || isset($_POST['2'])) // Check all of the forms to see if they were set, however, this ex. only has 2 forms
    
{
        if(
$_POST['1'] == '0')
        {
            echo 
$_POST['1'];
        }
        if(
$_POST['2'] == '0')
        {
            echo 
$_POST['2'];
        }
    }
}
?>

<form name="" action="">
    <select name="1">
        <option value="0">Select one...</option>
        <?
        $result 
mysql_query("SELECT * FROM sports") or die(mysql_error());
        
$num_rows mysql_num_rows($result);
        while(
$row mysql_fetch_array($result)){
            for(
$i 0$i $num_rows$i++)
            {
                print 
"<option value=\"".$row['points']."\">";
                print 
$row['name'];
                print 
"</option>";
            }
        }
        
?>
    </select>
    <select name="2">
        <option value="0">Select one...</option>
        <?
        $result 
mysql_query("SELECT * FROM sports") or die(mysql_error());
        
$num_rows mysql_num_rows($result);
        while(
$row mysql_fetch_array($result)){
            for(
$i 0$i $num_rows$i++)
            {
                print 
"<option value=\"".$row['points']."\">";
                print 
$row['name'];
                print 
"</option>";
            }
        }
        
?>
    </select>
    <input type="submit" name="submit" />
</form>
Hopefully you could figure that out. I THINK that's how you want it.

Closed Thread  
Page 1 of 2 1 2 >


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed