Lodius2000 Posted June 26, 2008 Share Posted June 26, 2008 i think this will be a softie I have an array called $styles and an array called $row $row has a numeric value called id I need to do some math with id and make the result of that math determine the value of the key to the $styles array heres my shot $styles[$row[id % 2]] $styles should have 2 values because of that, 0 and 1 those become the keys to the array to determine which element of the style array to use, the value of 0 or 1 so is nesting attempt correct? Link to comment https://forums.phpfreaks.com/topic/111960-solved-o-so-simple-nesting-arrays-question/ Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 Are you trying to do zebra stripes for a table? Or can you just give us an idea of what you're trying to do? Link to comment https://forums.phpfreaks.com/topic/111960-solved-o-so-simple-nesting-arrays-question/#findComment-574657 Share on other sites More sharing options...
Lodius2000 Posted June 26, 2008 Author Share Posted June 26, 2008 exactly danny looking in a book i have could it be $styles[$row][id % 2] ??? Link to comment https://forums.phpfreaks.com/topic/111960-solved-o-so-simple-nesting-arrays-question/#findComment-574659 Share on other sites More sharing options...
trq Posted June 26, 2008 Share Posted June 26, 2008 so is nesting attempt correct? Try it and see. Link to comment https://forums.phpfreaks.com/topic/111960-solved-o-so-simple-nesting-arrays-question/#findComment-574662 Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 I have no idea where you got your code from. When I do zebra stripes, it's unrelated to the query's array. I do... $n = 0; while($row = mysql_fetch_assoc($result)) { extract($row); if($n%2 == 0) $bgcolor="whatever color"; else $bgcolor="other color"; echo "<td bgcolor=$bgcolor>text</td>"; } and modify as needed. This is provided you only need alternating colors. If you're looking for different colors based on the content, let us know. Link to comment https://forums.phpfreaks.com/topic/111960-solved-o-so-simple-nesting-arrays-question/#findComment-574676 Share on other sites More sharing options...
trq Posted June 26, 2008 Share Posted June 26, 2008 I have no idea where you got your code from. When I do zebra stripes, it's unrelated to the query's array. I do.. Just to fix your code before I comment, your not incrementing the $n variable.... $n = 0; while($row = mysql_fetch_assoc($result)) { extract($row); if($n%2 == 0) $bgcolor="whatever color"; else $bgcolor="other color"; echo "<td bgcolor=$bgcolor>text</td>"; $n++; } The op's method relies on each row having odd/even numbers in sequence. Your method relies on the $n variable being incremented one number (odd followed by even) at a time. $styles = array('ddd','eee'); if ($result = mysql_query("SELECT id,data FROM foo")) { if (mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { echo "<td bgcolor='" . $styles[$row['id'] % 2] . "'>" . $row['data'] . "</td>"; } } } The first method is obviously more reliable as there is no guarantee that the id field will contain odd/even increments. Link to comment https://forums.phpfreaks.com/topic/111960-solved-o-so-simple-nesting-arrays-question/#findComment-574686 Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 yeah my bad not having the n incrementing Link to comment https://forums.phpfreaks.com/topic/111960-solved-o-so-simple-nesting-arrays-question/#findComment-574687 Share on other sites More sharing options...
trq Posted June 26, 2008 Share Posted June 26, 2008 yeah my bad not having the n incrementing Simple typo I'm sure, I just had to fix it before I could explain the different approuches. Link to comment https://forums.phpfreaks.com/topic/111960-solved-o-so-simple-nesting-arrays-question/#findComment-574689 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.