PHP form on function with SQL request -
i have function on php:
function getlastmatchs($nb) { try { $db = new pdo(dbhost, dbuser, dbpass); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); } catch(pdoexception $e) { die('connexion failed: '.$e->getmessage()); } $i=0; $get5tmatchs = $db->query('select wid, lid, date, cwid, clid `match`'); while ($nb<$i) { $data5matchs = $get5tmatchs->fetch(); echo '<tr> <td>'.$data5matchs['wid'].'</td>'; echo '<td>'.$data5matchs['lid'].'</td>'; echo '<td>'.$data5matchs['cwid'].'</td>'; echo '<td>'.$data5matchs['clid'].'</td>'; echo '<td>'.$data5matchs['date'].'</td> <br> </tr>'; $i++; } } and form is:
echo '<form action="index.php" method="post"> <h3>my question......</h3> <p> <input type="text" name="nbmatchs" /> <input type="submit" value="ok" /> </p> </form>'; echo getlastmatchs('nbmatchs'); how can show nbmatch time guys want table ? when now, nothing happen. help
ps: exemple tape 5, can see 5 time tabe have put in function.
what indended accomplish (as far understood) allow visitor enter numer , submit after "matches" data shown. number visitor entered acts limiter.
1. post variables? have placed function below form input value of string 'nbmatchs'. guess wanted submit form , 'nbmatches' value , apply sql query filtering. way have done doesn't work. have action attribute on form element set index.php. that's going submit form data. need have way submitted post variables. this:
$nbmatchs = $_post['nbmatchs']; never trust data client has given you. know must number let's check on it:
$nbmatches = is_numeric(trim($_post['nbmatchs'])) ? $_post['nbmatchs'] : 1; above checked if data client has given number. if we'll assign nubmer variable $nbmatches. if data client has given not number (eg. string) assign number 1 variable. @ point may end script execution let visitor know must enter number assign 1 variable if seems suspicious. after can submit variable function getlastmatchs takes variable , assigns sql query results limiter. assuming code in 1 file 'index.php' should have following code:
<?php function getlastmatchs($nbmatches) { try{ $db = new pdo(dbhost, dbuser, dbpass); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); } catch(pdoexception $e) { die('connexion failed: '.$e->getmessage()); } try { $select = $db->prepare('select wid, lid, date, cwid, clid `match` limit '.$nbmatches.';'); $select->execute(); $results = $select->fetchall(pdo::fetch_assoc); } catch(pdoexception $ex) { echo "<span style='color:red'>".$ex->getmessage()."</span></p>"; } echo '<table>'; foreach($results $result){ $output = '<tr>'; $output .= '<td>'.$result['wid'].'</td>'; $output .= '<td>'.$result['lid'].'</td>'; $output .= '<td>'.$result['cwid'].'</td>'; $output .= '<td>'.$result['clid'].'</td>'; $output .= '<td>'.$result['date'].'</td>'; $output = '</tr>'; echo $output; } echo '</table>'; } if(isset($_post['nbmatchs'])){ $nbmatches = is_numeric(trim($_post['nbmatchs'])) ? $_post['nbmatchs'] : 1; getlastmatchs($nbmatches); } ?> <form action="index.php" method="post"> <h3>my question......</h3> <p> <input type="text" name="nbmatchs" /> <input type="submit" value="ok" /> </p> </form> let me know if works way wanted.
Comments
Post a Comment