OnlyLeif Posted July 24, 2007 Share Posted July 24, 2007 I have a script that count and print every week in a selected year. Now i want to connect this to the returned result from a MySQL query. Code to explain what I am trying to accomplish: // $number_weeks = weeks in chosen year for($i = 1;$i <= $number_weeks;$i++) { $row = mysql_fetch_array($result); if ($row['week'] == $i) { echo "Exsisting in database!"; } } Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 24, 2007 Share Posted July 24, 2007 here u go <?php $row = mysql_fetch_array($result); for($i = 1;$i <= $number_weeks;$i++) { if ($row['week'] == $i) { echo "Exsisting in database!"; } } ?> Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted July 24, 2007 Share Posted July 24, 2007 OK - I can see a few uses for this kind of iteration - the best solution for you depends on what you are actually going to output info - but lets go on the basis you just want to show if the week is in the database... first you need the query result in an array so... <?php $qry = "SELECT * FROM `urtable`"; // swap this for your query string. $qry = mysql_query($qry); if (mysql_num_rows($qry) > 0) { $dataarr = array(); while($row = mysql_fetch_assoc($qry)) { foreach($row as $key => $val) { $dataarr[$key][] = $val; } } } ?> OK so we no have an array of all the results (hope its not MASSIVE!!!!) now all we need to do is see if the current weeknumber is in the corresponding array... <?php // $number_weeks = weeks in chosen year for($i = 1;$i <= $number_weeks;$i++) { if (array_keys($dataarr['week'],$i)) { echo "Exsisting in database!"; } } ?> Quote Link to comment Share on other sites More sharing options...
OnlyLeif Posted July 24, 2007 Author Share Posted July 24, 2007 This solution is for making those weeks (who are containing text and such) I am printing in a table becoming marked. It's as easy as that. So I wanted to identify who contained material and which who did not. vbnullchar: Your solution was not working. ToonMariner: Interresting approach, can´t get it working though. It returns: Warning: array_keys() [function.array-keys]: The first argument should be an array Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted July 24, 2007 Share Posted July 24, 2007 you need to have a field called 'week' in the result set - if its called something else change the $dataarr['week'] to what ever it is.... Quote Link to comment Share on other sites More sharing options...
OnlyLeif Posted July 24, 2007 Author Share Posted July 24, 2007 It was very simple. I didn´t have the variable-names in english but translated between my code and my postings here in the forum. I simply forgot to translate one variable. Thank you all for the help! 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.