rsleventhal Posted October 15, 2012 Share Posted October 15, 2012 (edited) I'm in a quandry. I have a series of records in a table. I'm allowing NULLs on my varchar columns, because the imported data may not have values for some of them. I need to manipulate each record with the end-goal of creating another query which will be used to insert data into another table. The first table, with the NULLS permitted, is the one I'm currently traversing with a 'while loop': $sql = "select * from " . DB_TBL; $result = mysql_query($sql); while ($data = mysql_fetch_assoc($result)) { //my foreach loop goes here } Pretty straightforward so far. So, I thought, was my foreach loop: foreach ($data as $key => $value) { if ((strlen($value) != 0)) { echo "key: " . $key . ': =' . $value . '<br />'; } else { echo '<br />'; } } When I run that, I seem to lose the first column, which is never NULL or strlen=0. It just doesn't display. Should this be done another way? Perhaps getting the numrows first, then doing the foreach, incrementing the row inside a for/next loop? thanks in advance for any productive ideas. Edited October 15, 2012 by rsleventhal Quote Link to comment https://forums.phpfreaks.com/topic/269499-foreach-testing-for-strlenkey-within-a-while/ Share on other sites More sharing options...
Jessica Posted October 15, 2012 Share Posted October 15, 2012 Add a trim() Quote Link to comment https://forums.phpfreaks.com/topic/269499-foreach-testing-for-strlenkey-within-a-while/#findComment-1385376 Share on other sites More sharing options...
ManiacDan Posted October 15, 2012 Share Posted October 15, 2012 Are you sure it's losing the first column? Does the first column have HTML in it? Do you see it when you run the SQL query by hand? Quote Link to comment https://forums.phpfreaks.com/topic/269499-foreach-testing-for-strlenkey-within-a-while/#findComment-1385380 Share on other sites More sharing options...
berridgeab Posted October 15, 2012 Share Posted October 15, 2012 (edited) Try below first and see if the data is actually there in the first place foreach ($data as $key => $value) { echo "key: " . $key . ': =' . $value . '<br />'; } Edited October 15, 2012 by berridgeab Quote Link to comment https://forums.phpfreaks.com/topic/269499-foreach-testing-for-strlenkey-within-a-while/#findComment-1385382 Share on other sites More sharing options...
rsleventhal Posted October 16, 2012 Author Share Posted October 16, 2012 Thanks to all who replied. @berridgeab: yes, all the data is there when I just dump it out like that. @ Jessica: adding trim(), while a good idea and something I've now done, does not affect the output @ManiacDan: I've run the query by hand in shell and all's as expected. The column I'm losing is always the first column and contains only plain text, no HTML. I have a feeling I'm doing something earlier in the code that's affecting the output, so I'll look there and continue debugging. Will post the solution once i find it. Again, my thanks, -R Quote Link to comment https://forums.phpfreaks.com/topic/269499-foreach-testing-for-strlenkey-within-a-while/#findComment-1385540 Share on other sites More sharing options...
ManiacDan Posted October 16, 2012 Share Posted October 16, 2012 There is no "earlier in the code" for this example, you select data and then print it. If you're not showing us your actual code, there's no way we can help you. You say the data is there when you remove the IF check. That implies that your IF check is failing. Inside that IF check, print out "In the if because strlen('{$value}') is zero<br />"; That should work. your thread title says $key, your code says $value. Are you sure you're checking the right variable? Quote Link to comment https://forums.phpfreaks.com/topic/269499-foreach-testing-for-strlenkey-within-a-while/#findComment-1385542 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.