scriptsit Posted May 31, 2010 Share Posted May 31, 2010 I'm a little stuck with a project, and if anyone could help, it'd be much appreciated. I have a situation where in an html form, I have a variable number of items being sent through post. The number is dependent on a database listing. Because I am also dealing with post values I'm expecting, I have to sift through them. How I approached the problem: foreach($_POST as $k => $v){ if(strcmp($k, 'day') == 0){ echo "got day"; <snip> } } Basically what this SHOULD do is to look at each of the keys submitted, compare each one with 'day' or a few other values that I'm expecting, and what's left in my 'else' will be the variable number of items, and I can handle them accordingly. However, what this DOES do is exit without printing a sing value. Debugging has shown me that the error in this code is here or in one of the other elseif's. Does anyone have any input? I am totally stuck, and I've been looking at the problem for way too long to be productive. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/203430-strcmp-help/ Share on other sites More sharing options...
kenrbnsn Posted May 31, 2010 Share Posted May 31, 2010 Please post your form. Link to comment https://forums.phpfreaks.com/topic/203430-strcmp-help/#findComment-1065736 Share on other sites More sharing options...
scriptsit Posted May 31, 2010 Author Share Posted May 31, 2010 <form action="adder.php" method="post" > <table> <tr><td>Week Ending:</td><td><?php put_select_date(); ?></td></tr> <tr><td></td><td><strong>Update Miles</strong></td></tr> <?php $query = "select * from trucks;"; $result = $db->query($query); $num = $result->num_rows; for($i = 0; $i < $num; $i++){ $row = $result->fetch_assoc(); echo "<tr><td>Truck" . $row['number'] . "</td>"; echo "<td><input type=\"text\" name=\"" . $row['truckid'] . "\"></td></tr>"; } ?> Link to comment https://forums.phpfreaks.com/topic/203430-strcmp-help/#findComment-1065740 Share on other sites More sharing options...
scriptsit Posted May 31, 2010 Author Share Posted May 31, 2010 Sorry, I didn't actually put the whole form on there, just what I thought was relavent. The put_select_date(); function is one of mine, which basically gives a date selection input with today's date already highlight. The values sent are 'day', 'month', and 'year'. The rest of the form is as follow: <tr><td></td><td><Strong>Update Fuel Meter</strong></td></tr> <tr><td>This week</td><td><input type="text" name="gal" />gal</td></tr> <tr><td>Price</td><td><input type="text" name="price" /></td></tr> <tr><td></td><td><input type="submit" value="Enter" /></td></tr> </table> </form> Link to comment https://forums.phpfreaks.com/topic/203430-strcmp-help/#findComment-1065745 Share on other sites More sharing options...
kenrbnsn Posted May 31, 2010 Share Posted May 31, 2010 At the top of your script that processes the form, put <?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?> This will dump what is being sent from the form. Also, you can re-write you loop as <?php while ($row = $result->fetch_assoc()){ echo "<tr><td>Truck" . $row['number'] . "</td>"; echo "<td><input type='text' name='" . $row['truckid'] . "'></td></tr>"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/203430-strcmp-help/#findComment-1065748 Share on other sites More sharing options...
scriptsit Posted May 31, 2010 Author Share Posted May 31, 2010 The output (after commenting out bad code): Array ( [month] => 05 [day] => 30 [year] => 10 [1] => 100100 [2] => 120100 [gal] => 100100 [price] => 3.21 ) Link to comment https://forums.phpfreaks.com/topic/203430-strcmp-help/#findComment-1065752 Share on other sites More sharing options...
kenrbnsn Posted May 31, 2010 Share Posted May 31, 2010 In your "if" statement, why don't you just do: <?php if($k == 'day'){ echo "got day"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/203430-strcmp-help/#findComment-1065757 Share on other sites More sharing options...
scriptsit Posted May 31, 2010 Author Share Posted May 31, 2010 To be honest, I thought I had an error once yielded by using == instead of strcmp. It may have been, incidentally, an error in that program, but since then I've been using strcmp to satisfy incidents where I would have used perl's 'eq'. I'm not sure why your solution worked, but it has. And, since I do not like to wrestle with fortune, I shall be content knowing that it works (at least until memorial day is over). Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/203430-strcmp-help/#findComment-1065767 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.