Jump to content

is_numeric()


bravo14

Recommended Posts

Hi

 

Using the array below I am populating a select field

 

 

 
$scoring=array(
'3' =>  'First',
'2' =>  'Second',
'1' =>  'Third',
'0' =>  'Fourth',
'R' =>  'Retired or mechanical failure',
'F' => 'Fell',
'FN'=> 'Fell and non-starter in re-run of race',
'N2'=> 'Exclusion for exceeding two minute time allowance',
'E' => 'Exclusion for starting infringement',
'FX'=> 'Fell and excluded from re-run of race',
'X' =>  'Other exclusion',
'N' => 'Replaced by a reserve, or non-starter',
'-'=> 'No ride');

 

 

 
<select name="points[]" class="txtfield">
  <option value="">-----------------</option>
<?php
foreach($scoring as $key=> $value):
echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!!
endforeach;
?>
  </select>

 

on submit I then use the following code, checking if the value of `points` is numeric, and if not set pts to be 0 and add the posted value to a different field.

 

 

 
<?php
if(isset($_POST['submit-scores'])){
$heat=$_POST['heat'];
$next_heat=$_POST['nextheat'];
$card=$_POST['card'];
 for($i=0;$i<4;$i++){
 //set rider variables
 $id=$_POST['id'];
 $points=$_POST['points'];
 if(is_numeric($points)){
 $DNF = NULL;
 $pts=$points;}
 else{
 $pts=0;
 $DNF = $points;
 }
 $rider=$_POST['rider'];
 if(isset($_POST['substitute'])){
 $sub=$_POST['sub'];
 $subpts=$_POST['subpts'];
 }
 else{
  $sub=NULL;
 $subpts=NULL;
 }
 //update database
 $sql1="UPDATE tbl_heat SET rider_name='$rider[$i]', points='$pts[$i]', DNF= '$DNF[$i] ', substitute='$sub[$i]', sub_points='$subpts[$i]' WHERE id='$id[$i]'";
 echo $sql1.'<br/>';
 //$result1=mysql_query($sql1)or die(mysql_error());
 }
 //update card info
 $sql="Update tbl_card SET next_heat = $next_heat where `card_id` = $card";
 //$result=mysql_query($sql)or die(mysql_error());
 //header ('Location: score-card.php?card='.$card.'#'.$nextheat);
 }
?>

 

when I echo the query I get the following result

 

UPDATE tbl_heat SET rider_name='Chris Harris', points='', DNF= '3 ', substitute='', sub_points='' WHERE id='853'
UPDATE tbl_heat SET rider_name='Kenneth Bjerre', points='', DNF= '2 ', substitute='', sub_points='' WHERE id='854'
UPDATE tbl_heat SET rider_name='Martin Smolinski', points='', DNF= '1 ', substitute='', sub_points='' WHERE id='855'
UPDATE tbl_heat SET rider_name='Chris Holder', points='', DNF= 'F ', substitute='', sub_points='' WHERE id='856'

 

it appears to be adding a space to the end of each of the array keys, therefore saying that 3 is not numeric etc, so a cople of things, why is the space being added, secondly is this the best way to achieve the result?

Link to comment
Share on other sites

I have changed the submit code to 

 

 

 
<?php
if(isset($_POST['submit-scores'])){
$heat=$_POST['heat'];
$next_heat=$_POST['nextheat'];
$card=$_POST['card'];
 for($i=0;$i<4;$i++){
 //set rider variables
 $id=$_POST['id'];
 $points=$_POST['points'];
 echo $points;
 if($points=='3'){
 $pts = 3;
 $DNF = NULL;
 }
 elseif($points=='2'){
 $pts = 2;
 $DNF = NULL;
 }
 elseif($points == '1'){
 $pts = 1;
 $DNF = NULL;
 }
 elseif($points == '0'){
 $pts = 0;
 $DNF = NULL;
 }
 else{
 $pts = 0;
 $DNF = $points;
 }
 
 $rider=$_POST['rider'];
 if(isset($_POST['substitute'])){
 $sub=$_POST['sub'];
 $subpts=$_POST['subpts'];
 }
 else{
  $sub=NULL;
 $subpts=NULL;
 }
 //update database
 $sql1="UPDATE tbl_heat SET rider_name='$rider[$i]', points='$pts[$i]', DNF= '$DNF[$i]', substitute='$sub[$i]', sub_points='$subpts[$i]' WHERE id='$id[$i]'";
 echo $sql1.'<br/>';
 //$result1=mysql_query($sql1)or die(mysql_error());
 }
 //update card info
 $sql="Update tbl_card SET next_heat = $next_heat where `card_id` = $card";
 //$result=mysql_query($sql)or die(mysql_error());
 //header ('Location: score-card.php?card='.$card.'#'.$nextheat);
 }
?>

 

but I am now getting

 

ArrayUPDATE tbl_heat SET rider_name='Chris Harris', points='', DNF= '3', substitute='', sub_points='' WHERE id='853'
ArrayUPDATE tbl_heat SET rider_name='Kenneth Bjerre', points='', DNF= '2', substitute='', sub_points='' WHERE id='854'
ArrayUPDATE tbl_heat SET rider_name='Martin Smolinski', points='', DNF= '1', substitute='', sub_points='' WHERE id='855'
ArrayUPDATE tbl_heat SET rider_name='Chris Holder', points='', DNF= 'F', substitute='', sub_points='' WHERE id='856'

 

I still cant get it to update the points with a value

Link to comment
Share on other sites

since your form field is an array, and you apparently have 4 similar select menus, you must use array notation/functions to access the elements of the array.

 

you can either use your for($i=0;$i<4;$i++){ loop index $i variable - $points=$_POST['points'][$i]; or just use a foreach loop foreach($_POST['points'] as $points){ ... }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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