Krewe Posted November 11, 2011 Share Posted November 11, 2011 Hey Guys, just curious if there is away to do this so I can shorten my code by 25 functions . I need a bit of code like $count = 1. Then at the end of the function/while loop the count is ++.What I want is a $count that is = a. then at the end of the while loops goes to b, then c, then d... etc.Would I just make an array? Something like, $letter = array('a' => 1, 'b' => 2, 'c' => 3, 'd => 4', 'e' => 5, 'f' => 6, 'g' => 7,'h'=> 8, 'i' => 9, 'j' => 10, 'k' => 11, 'l' => 12, 'm' => 13, 'n' => 14,'o' => 15, 'p' => 16, 'q' => 17, 'r' => 18, 's' => 19, 't' => 20, 'u' => 21, 'v' => 22, 'w' => 23, 'x' => 24, 'y' => 25, 'z' => 26); $count = 1;while($count <= 26){//This should echo letter a, then b, then c, etc...echo $letter['{$count}'];$count++} But it doesn't work... Link to comment Share on other sites More sharing options...
Don E Posted November 11, 2011 Share Posted November 11, 2011 I think in order to access the values of associative arrays, you have to use the id/index key, in this case for you it's 'a' and its value is 1. So with the way you have it: echo $letter['{$count}']; ... I don't think it'll work because what you're doing there is echoing the value of $count, which would be 1. So what I think would be better is if you switch the array around to look like this: $letter = array(1 => 'a', 2 =>'b', 3 =>'c', etc etc So in this way when you loop through, you it should do what you're trying to accomplish. Link to comment Share on other sites More sharing options...
Krewe Posted November 11, 2011 Author Share Posted November 11, 2011 Alright done that fixed it! Just had to change the echo to: echo $letter[$count]; (Remove the ' ' and { }) Thanks bud Link to comment Share on other sites More sharing options...
Don E Posted November 11, 2011 Share Posted November 11, 2011 Welcome. Nice yogurt site by the way. Link to comment Share on other sites More sharing options...
Krewe Posted November 11, 2011 Author Share Posted November 11, 2011 (Thanks for looking at my site, all of this work right now is going into the new layout... Http://www.surfshack....com/testing3.0) Ok. Now that i got that part figured out I am running into a snag... <?php$letters = array(1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd', 5 => 'e', 6 => 'f', 7 => 'g', 8 => 'h', 9 => 'i', 10 => 'j', 11 => 'k', 12 => 'l', 13 => 'm', 14 => 'n', 15 => 'o', 16 => 'p', 17 => 'q', 18 => 'r', 19 => 's', 20 => 't', 21 => 'u', 22 => 'v', 23 => 'w', 24 => 'x', 25 => 'y', 26 => 'z');$count = 1;while($count <= 26){$result = get_toppings_by_letter($letters[$count]);$data = mysql_fetch_array($result); if(mysql_num_rows($result) != 0){echo "<h2>" . $letters[$count] . "</h2><ul style=\"list-style-type:disc;\">";while($data = mysql_fetch_array($result)){ echo "<li>" . $data['topping_name'] . "</li>";}}$count++;echo "</ul>";}?> This is my bit of code that will grab all the toppings from the database, file them under a letter, and then move on. However, when I run the if statement checking if there are any toppings starting with a letter it successfully grabs all the Sections that have toppings, however it takes away the very first result. If I comment out the if all the sections are there (a-z) but no toppings are taking off either. So I know it is the if statement... If you actually go to Http://www.surfshack....0/toppings.php you can see the list without the if.Http://www.surfshackyogurt.com/testing3.0/toppingsif.php is WITH the if. Link to comment Share on other sites More sharing options...
Don E Posted November 11, 2011 Share Posted November 11, 2011 I think the problem is the first call to mysql_fetch_array. I modified the code above a little with what you can try. All I did was basically remove(commented out) the first call and added the greater than operator to the mysql_num_rows 'if' check: <?php$letters = array(1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd', 5 => 'e', 6 => 'f', 7 => 'g', 8 => 'h', 9 => 'i', 10 => 'j', 11 => 'k', 12 => 'l', 13 => 'm', 14 => 'n', 15 => 'o', 16 => 'p', 17 => 'q', 18 => 'r', 19 => 's', 20 => 't', 21 => 'u', 22 => 'v', 23 => 'w', 24 => 'x', 25 => 'y', 26 => 'z');$count = 1;while($count <= 26){$result = get_toppings_by_letter($letters[$count]);//$data = mysql_fetch_array($result);if(mysql_num_rows($result) > 0){ echo "<h2>" . $letters[$count] . "</h2><ul style=\"list-style-type:disc;\">"; while($data = mysql_fetch_array($result)) { echo "<li>" . $data['topping_name'] . "</li>"; }}echo "</ul>";$count++;}?> So basically it checks to see if any rows were returned from the query from get_toppings_by_letter function that is stored into $result. I'm sure you can also use != 0 here as well. You can also do this if you want. Does the same thing: $num_rows = mysql_num_rows($result);if($num_rows > 0){ ..... Link to comment Share on other sites More sharing options...
Krewe Posted November 11, 2011 Author Share Posted November 11, 2011 Aha! You're the best. Works perfectly now. Link to comment Share on other sites More sharing options...
Synook Posted November 12, 2011 Share Posted November 12, 2011 You may also be interested in the chr() function . Link to comment Share on other sites More sharing options...
Krewe Posted November 12, 2011 Author Share Posted November 12, 2011 Wow Synook. That eliminates the array completely! Nice.... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.