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
https://forums.phpfreaks.com/topic/288212-is_numeric/
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
https://forums.phpfreaks.com/topic/288212-is_numeric/#findComment-1478083
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
https://forums.phpfreaks.com/topic/288212-is_numeric/#findComment-1478088
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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