saedm Posted May 16, 2008 Share Posted May 16, 2008 Hello, When I execute this script what happens is that every field in the table is repeated twice, where am I messing up?? $questions_datasource = mysql_query("SELECT * FROM table"); while($question = mysql_fetch_array($questions_datasource)) { echo "<tr>"; foreach($question as $data) //for($i = 0; $i < sizeof($question); $i++) { echo "<td>{$data}</td>"; } echo "</tr>"; } If I replace foreach with: for($i = 0; $i < sizeof($question); $i++) it works fine. But in another script I tried foreach and it did the same, but I replace with for like this: $edit_question = mysql_query("SELECT * FROM intro WHERE id = {$_GET['ID']}"); while($question = mysql_fetch_array($edit_question)) { for($i = 0; $i < sizeof($question); $i++) { $field_name = mysql_field_name($edit_question, $i); echo "<tr><td>$field_name</td><td>$data</td><td><input type=\"textarea\" /></td></tr>"; } } What happened is it printed all the array contents in rows, but when it reached the final row it didn't finish it printed a double more textareas. What the hell.. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/105904-foreach-repeating-the-data-twice-from-mysql/ Share on other sites More sharing options...
MadTechie Posted May 16, 2008 Share Posted May 16, 2008 sizeof AKA count returns the number of items, in an array, the array starts from 0, so you need to remove one from the count, see below <?php $edit_question = mysql_query("SELECT * FROM intro WHERE id = {$_GET['ID']}"); while($question = mysql_fetch_array($edit_question)) { for($i = 0; $i < (sizeof($question)-1); $i++) { $field_name = mysql_field_name($edit_question, $i); echo "<tr><td>$field_name</td><td>$data</td><td><input type=\"textarea\" /></td></tr>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105904-foreach-repeating-the-data-twice-from-mysql/#findComment-542709 Share on other sites More sharing options...
saedm Posted May 16, 2008 Author Share Posted May 16, 2008 no... it's $i < not <= array size 1 $i = 0; $i = 1 (doesn't work) but thanks anyway What's the thing with foreach? Any idea I might have messed up somewhere else? Quote Link to comment https://forums.phpfreaks.com/topic/105904-foreach-repeating-the-data-twice-from-mysql/#findComment-542713 Share on other sites More sharing options...
saedm Posted May 16, 2008 Author Share Posted May 16, 2008 i did echo sizeof($question) and it printed 30, and there are 15 in the database...ha?! Quote Link to comment https://forums.phpfreaks.com/topic/105904-foreach-repeating-the-data-twice-from-mysql/#findComment-542717 Share on other sites More sharing options...
MadTechie Posted May 16, 2008 Share Posted May 16, 2008 mysql_fetch_array does both associative and indexd so 30 is correct try this $edit_question = mysql_query("SELECT * FROM intro WHERE id = {$_GET['ID']}"); while($question = mysql_fetch_assoc($edit_question)) { foreach($question as $K => $V) { $field_name = mysql_field_name($edit_question, $i); echo "<tr><td>$K</td><td>$V</td><td><input type=\"textarea\" /></td></tr>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/105904-foreach-repeating-the-data-twice-from-mysql/#findComment-542721 Share on other sites More sharing options...
paul2463 Posted May 16, 2008 Share Posted May 16, 2008 I have just been playing with it as it was printing the data twice for me too until i changed this line while($question = mysql_fetch_array($questions_datasource)) to while($question = mysql_fetch_assoc($questions_datasource)) might be worth a go Quote Link to comment https://forums.phpfreaks.com/topic/105904-foreach-repeating-the-data-twice-from-mysql/#findComment-542726 Share on other sites More sharing options...
saedm Posted May 16, 2008 Author Share Posted May 16, 2008 hehe, while I was trying to find something I tried mysql_fetch_row, instead of array, and it worked fine! it printed 15 Thanks for the assoc! it works too! but it's associative so that's a bit of a problem(not with foreach though) i did: echo "<pre>"; print_r($question); echo "</pre>"; this is what is printed: Array ( [0] => 1 [id] => 1 [1] => www.google.com [question] => www.google.com [2] => www.zzz.com [answer] => www.zzz.com [3] => recursion [notes] => recursion [4] => recursion [tags] => recursion [5] => 2000 [year] => 2000 [6] => saher [people] => saher [7] => 5 [rating] => 5 [8] => 5 [rators_num] => 5 [9] => al [moed] => al [10] => 1 [question_num] => 1 [11] => 1 [question_part] => 1 [12] => 2 [times_visited] => 2 [13] => 1 [recommend] => 1 [14] => the pizpiz [question_name] => the pizpiz ) so yea you are right. But is this what is supposed to happen, how weird I never ran into this problem before. Well what do you use as a php programmer? mysql_fetch_assoc? Thanks a lot for your replies Quote Link to comment https://forums.phpfreaks.com/topic/105904-foreach-repeating-the-data-twice-from-mysql/#findComment-542729 Share on other sites More sharing options...
MadTechie Posted May 16, 2008 Share Posted May 16, 2008 did you even try my last post ? Quote Link to comment https://forums.phpfreaks.com/topic/105904-foreach-repeating-the-data-twice-from-mysql/#findComment-542731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.