petewall Posted March 22, 2014 Share Posted March 22, 2014 hey, i have a little problem, i'm fetching data from a table with 20x3 columns. i've namned them j1 -> j20, k1 -> k20, z1 -> z20. i need to check wether j1 -> j20 is empty and that k1 -> k20 is numeric. and if both these criteria are not met, none of j, k or z should be outputted. i've tried with if(!empty($row['j1']) && is_numeric($row['k1'])) { echo $row['z1']." "; echo $row['k1']." "; echo $row['j1']; echo "<br />"; } if(!empty($row['j2']) && is_numeric($row['k2'])) { echo $row['z2']." "; echo $row['k2']." "; echo $row['j2']; echo "<br />"; } it works, but theres just ALOT of code for something small and i'm wondering if there's another way to do this, maybe with a for-loop or something? i tried with: for ($j = 1; $j <= 20; $j++) { if(!empty($row['j1$j'])) { echo $row['z$j']." "; echo $row['k$j']." "; echo $row['j$j']; echo "<br />"; } } But i'm guessing you can't have the $j inside the $row['']. Anyone got any ideas? on how to make this piece of code more simple? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 22, 2014 Share Posted March 22, 2014 the solution to simplifying the logic needed to manipulate the data will be to correct the design of your database table. database tables should be designed to store data using one row per related data set. just guessing but you likely need three columns (whatever the corresponding/related j, k, and z values are), with 20 rows (or whatever number of actual data sets there are.) Quote Link to comment Share on other sites More sharing options...
Solution petewall Posted March 22, 2014 Author Solution Share Posted March 22, 2014 I actually solved it with for ($j = 1; $j <= 20; $j++) { if(!empty($row['j'.$j]) && is_numeric($row['k'.$j])) { echo $row['z'.$j]." "; echo $row['k'.$j]." "; echo $row['j'.$j]; echo "<br />"; } } Quote Link to comment 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.