PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

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 Codewalkers Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old May 6th, 2008, 03:22 PM
siriusdave siriusdave is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 19 siriusdave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 48 m 24 sec
Reputation Power: 0
Loop help

hi all can you help me with a loop

i have this form

<form method="POST" action="makepreds.php?a=add_preds">
foreach ( $preds as $p) {
<input type="text" name="matchid" value="'. $p['matchid'].'">
<input type="text" name="userid" value="'. $user['id'].'">
<input type="text" name="predtime" value="'.$p['predtime'].'">
}
<input class="submit" type="SUBMIT" name="Predict" value="add">

and this :
$preds_action = $_GET['a'];
if ($preds_action == 'add_preds') {
if (isset($_POST['Predict'])) {
$insert_shout_query = mysql_query("INSERT ". $tbl_prefix ."predictiondata SET userid = '".$_POST['userid']."', pmatchid = '".$_POST['matchid']."', predtime ='".$_POST['predtime']."', ");
}
}


but it only enter the last one in the form how do i loop it so it will enter all the input as the is 10 of each field...

Thank you siriusdave...

Reply With Quote
  #2  
Old May 6th, 2008, 04:55 PM
MatthewJ MatthewJ is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 163 MatthewJ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 7 h 2 m 29 sec
Reputation Power: 2
Dave,

The way it is running now, you are going to end up with 10 fields of each, but all ten of each item will have the same name... So, even though there are 10 fields, the second one overwrites the first, then the third the second, and so on... If you want them to be 10 distinct fields, you need to add a value to the name... so the first time through the loop you would get matchid1, userid1, predtime1, then the second time, matchid2, userid2, predtime2, etc.

I would probably add a counter to the loop, then use the counter to add the number to each field...

You of course would then need to run the same counter on the insert query, so it will also run ten times, and insert each one of the "sets" of data.

I hope that makes more sense to read than it did to type,

Matt

Reply With Quote
  #3  
Old May 7th, 2008, 12:34 AM
siriusdave siriusdave is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 19 siriusdave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 48 m 24 sec
Reputation Power: 0
yes it did but not sure what code i need for the couter and where to put it...

Reply With Quote
  #4  
Old May 7th, 2008, 08:28 AM
MatthewJ MatthewJ is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 163 MatthewJ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 7 h 2 m 29 sec
Reputation Power: 2
Dave,

Something like this should work...

Let me know,

PHP Code:
<form method="POST" action="makepreds.php?a=add_preds">
<?
php $counter 0?>
<?php 
foreach ( $preds as $p) { ?>
<?php $counter 
+= 1?>
<input type="text" name="matchid<?php echo $counter?>" value="'. $p['matchid'].'">
<input type="text" name="userid<?php echo $counter?>" value="'. $user['id'].'">
<input type="text" name="predtime<?php echo $counter?>" value="'.$p['predtime'].'">
<?php ?>
<input class="submit" type="SUBMIT" name="Predict" value="add">

<?php 
$preds_action 
$_GET['a'];
if (
$preds_action == 'add_preds') {
if (isset(
$_POST['Predict'])) {
for(
$i=0$i $preds(count); $i++) {
$insert_shout_query mysql_query("INSERT "$tbl_prefix ."predictiondata SET userid = '".$_POST['userid'].$i."', pmatchid = '".$_POST['matchid'].$i."', predtime ='".$_POST['predtime'].$i."', ");
}
}
}
?> 

Reply With Quote
  #5  
Old May 7th, 2008, 03:21 PM
siriusdave siriusdave is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 19 siriusdave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 48 m 24 sec
Reputation Power: 0
does not work ....

these are the whole files if it helps .....

makepeds.php
PHP Code:
/**
 * siriusfootball 1.0.0
 * Copyright © 2006-2007 Siriusdave, All Rights Reserved
 **/
session_start();
require (
'includes/config.php');
require (
'templates/'$settings['template'] .'/makepreds.template.php');

global 
$settings,$tbl_prefix,$user ;

if (
$user['status'] != '1')
    
please_log_in();
    
    
$preds_action $_GET['a'];
if (
$preds_action == 'add_preds') {
if (isset(
$_POST['Predict'])) {
$insert_query mysql_query("INSERT "$tbl_prefix ."predictiondata SET lid = '0', userid = '".$_POST['userid']."', pmatchid = '".$_POST['matchid']."', predtime ='".$_POST['predtime']."', phomescore ='".$_POST['phomescore']."', pawayscore ='".$_POST['pawayscore']."' ");

}
}



   
$date getTodaysDate(date("Y-m-d"));
    
$week GetThisWeek();
    if (
array_key_exists("week"$_GET)) {
      
$week $_GET["week"];
    }


$match_query mysql_query("SELECT *,p.* FROM "$tbl_prefix ."matchdata AS m LEFT JOIN "$tbl_prefix ."predictiondata AS p ON (m.matchid = p.pmatchid AND p.userid='".$user['id']."' AND m.lid = p.lid ) WHERE m.week=$week AND m.lid='0' ORDER BY m.week, m.matchdate");

$preds = array();
while(
$match_row mysql_fetch_assoc($match_query)) {

$points 0;

$preds[] = array (
    
'matchid' => $match_row["matchid"],
    
'matchdate' => convertDatetimeToScreenDate($match_row["matchdate"]),
    
'hometeam' =>  $match_row["hometeam"],
    
'awayteam' => $match_row["awayteam"],
    
'homescore' => $match_row["homescore"], 
    
'awayscore' =>$match_row["awayscore"],
    
'phomescore' => $match_row["phomescore"], 
    
'pawayscore' =>$match_row["pawayscore"],
    
'week' => $week,
    
'PTS'=> $points,
    
'bonus' => $match_row["bonuspoints"],
    
'homeflag'=> $match_row["hometeam"],
    
'awayflag'=> $match_row["awayteam"],
    
'predtime'=>$date
    
);

}    
$page_title "Predictions";
// Load template
template_preds(); 


and makepreds.template.php


PHP Code:
/**
 * siriusfootball 1.0.0
 * Copyright © 2006-2007 Siriusdave, All Rights Reserved
 *
 * Template: Siriusdave by siriusdave
 **/


function template_preds() {
    global 
$settings$lang$preds,$user;

template_header2();

foreach ( 
$preds as  $p) {}
    echo 
'<div class="content_box_header">
      <a href="'
$settings['siteurl'] ,'/">'$settings['sitename'] ,'</a> >Predictions </div>
    <div class="content_box">
    <form method="POST" action="makepreds.php?a=add_preds">'
;

    echo
'
    <table align="center" width="100%" >
    <tr>
    <td colspan="11" align="center" class="TBLROWRED">Predictions  [Week '
.$p['week'].']</td>
    </tr>
    <tr>
    <td width="80" align="center" class="TBLROWRED">Date</td>
    <td class="TBLROWRED" >&nbsp;</td>
    <td width="150" align="center" class="TBLROWRED">Home</td>
    <td align="center" class="TBLROWRED">G</td>
    <td align="center" class="TBLROWRED">v</td>
    <td align="center" class="TBLROWRED">G</td>
    <td width="150" align="center" class="TBLROWRED">Away</td>
    <td class="TBLROWRED" >&nbsp;</td>
      <td align="center" class="TBLROWRED">Result</td>
    <td align="center" class="TBLROWRED">PTS</td>
    <td width="60" align="center" class="TBLROWRED">Bonus Points</td>
    </tr>'
;

    foreach ( 
$preds as  $p) {

       echo
' <tr>
      <td width="80" align="center" class="TBLROWGREEN">'
.$p['matchdate'].'</td>
      <td class="TBLROWGREEN"><img src="flags/'
.$p['homeflag'].'.png";"></td>
      <td width="150"align="center" class="TBLROWGREEN">'
.$p['hometeam'] .'</td>
      <td align="CENTER" class="TBLROWGREEN">'
;
      
 if(
$p['phomescore']==''){
echo
'<select name="phomescore">
<option></option>
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
</select>'
;
}else{
echo
''.$p['phomescore'].'';
}
echo
'<br></td>
      <td align="CENTER" class="TBLROWGREEN">v</td>
      <td align="CENTER" class="TBLROWGREEN">'
;
if(
$p['pawayscore']==''){
echo
' <select name="pawayscore" >
<option></option>
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
</select>'
;
}else{
echo
' '.$p['pawayscore'].'';

echo
'<br></td>
    <td width="150" align="center" class="TBLROWGREEN">'
.$p['awayteam'] .'</td>
    <td class="TBLROWGREEN"><img src="flags/'
.$p['awayflag'].'.png";"></td>
     <td align="center" class="TBLROWGREEN">'
.$p['homescore'] .' - '.$p['awayscore'] .' </td>
    <td align="center" class="TBLROWGREEN">'
.$p['PTS'].'</td>
      <td width="60" align="center" class="TBLROWGREEN">'
$p['bonus'].'</td>
      </tr>
      <input type="text" name="matchid" value="'
$p['matchid'].'">
      <input  type="text" name="userid" value="'
$user['id'].'">
<input  type="text" name="predtime" value="'
.$p['predtime'].'">'

}
echo
'<tr><td colspan="11" align="center" >
<input class="submit" type="SUBMIT" name="Predict" value="Predict These score">

</td></tr></table>
</form>
</div>'
;

echo 
'
</div>
<div style="clear: both;"></div>
</div><div style="clear: both;"></div>'
;

template_footer2();



Last edited by siriusdave : May 7th, 2008 at 03:26 PM.

Reply With Quote
  #6  
Old May 9th, 2008, 12:20 PM
siriusdave siriusdave is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 19 siriusdave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 48 m 24 sec
Reputation Power: 0
sorry to bump ...but anyone ?

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Loop help


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 | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway