phpdragon Posted April 3, 2009 Share Posted April 3, 2009 I want to create an incrementing letter starting at 'F' and incrementing x times specified in a previous variable. The loop below is where I want this to work and $letter is the variable I want to start at 'F' and increment each time. while ($newtt=mysql_fetch_array($newbot)) { $namebot=$newtb['name']; $catIDbot=$newtb['catID']; (function here for defining $letter) echo "<td bgcolor='#FF9999' width='150'><strong>=SUM(".$letter."5:".$letter.$restot.")</strong></td>"; } Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/ Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 Can you post some "before" and "after" data please - that's as clear as a muddy window! Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800567 Share on other sites More sharing options...
9three Posted April 3, 2009 Share Posted April 3, 2009 http://php.net/range Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800584 Share on other sites More sharing options...
phpdragon Posted April 3, 2009 Author Share Posted April 3, 2009 The code generates <td> in a table and fills it with the total of all the cells above it. That is from this code <?php $catbot="SELECT categories.category AS name, categories.categoryID AS catID, receipts.category AS checkID, receipts.transDate AS trans, receipts.companyID AS compID FROM categories LEFT JOIN receipts ON categories.categoryID=receipts.category WHERE receipts.category=categories.categoryID AND receipts.companyID='$bizID' AND receipts.transDate BETWEEN '$startDate' AND '$endDate' GROUP BY categories.category ORDER BY categories.category"; $newbot=mysql_query($catbot); while ($newtt=mysql_fetch_array($newbot)) { $namebot=$newtb['name']; $catIDbot=$newtb['catID']; (function needed here for $letter increment on each loop) echo "<td bgcolor='#FF9999' width='150'><strong>=SUM(".$letter."5:".$letter.$restot.")</strong></td>"; } ?> I need an incremented letter for $letter each time the while loop passes starting at 'F' hope that is a little clearer from 9three's link this code looks useful but not sure how to dynamically set the second letter value in this case i and adapt to my need in the while loop. // Use of character sequences introduced in 4.1.0 // array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'); foreach (range('a', 'i') as $letter) { echo $letter; } Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800603 Share on other sites More sharing options...
MatthewJ Posted April 3, 2009 Share Posted April 3, 2009 <?php $increment = 5; $letters = range('A', 'Z'); $start = array_search("F", $letters); $end = $start + $increment; echo $letters[$end]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800628 Share on other sites More sharing options...
laffin Posted April 3, 2009 Share Posted April 3, 2009 $letter=ord('F'); // Get character value of Letter for($i=0;$i<10;$i++,$letter++) echo chr($letter).' '; Simplest way I can think of Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800705 Share on other sites More sharing options...
Maq Posted April 3, 2009 Share Posted April 3, 2009 $letter=ord('F'); // Get character value of Letter for($i=0;$i echo chr($letter).' '; Simplest way I can think of Put this code to loop until 100 and see what it outputs... Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800779 Share on other sites More sharing options...
DarkWater Posted April 3, 2009 Share Posted April 3, 2009 ++ and -- work to increment and decrement letters, actually. Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800780 Share on other sites More sharing options...
phpdragon Posted April 4, 2009 Author Share Posted April 4, 2009 This is the closest thing I can get to sort of work but it loops on way past the number of results due to the fact it counts to z each time and then loops the whole lot for each sql result, I just want it to increment once for each loop of the sql result while loop. <?php $catbot="SELECT categories.category AS name, categories.categoryID AS catID, receipts.category AS checkID, receipts.transDate AS trans, receipts.companyID AS compID FROM categories LEFT JOIN receipts ON categories.categoryID=receipts.category WHERE receipts.category=categories.categoryID AND receipts.companyID='$bizID' AND receipts.transDate BETWEEN '$startDate' AND '$endDate' GROUP BY categories.category ORDER BY categories.category"; $newbot=mysql_query($catbot); while ($newtt=mysql_fetch_array($newbot)) { $namebot=$newtb['name']; $catIDbot=$newtb['catID']; for ($i='F';$i<'Z';$i++) { echo "<td bgcolor='#FF9999' width='150'><strong>=SUM(".$i."5:".$i.$restot.")</strong></td>";} } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800841 Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 ++ and -- work to increment and decrement letters, actually. Like I said, put it in a loop to 100 and see what it outputs. I'm sure the OP didn't want ™ in his list of characters, actually. Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800843 Share on other sites More sharing options...
phpdragon Posted April 4, 2009 Author Share Posted April 4, 2009 I am not sure what you mean, I just want it to loop for each instance of $catIDbot and increment 1 letter each time so I can use that letter as a variable in the echo statement that loops my <td> tag. could you show me an example of what you mean related to my loop here Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800854 Share on other sites More sharing options...
DarkWater Posted April 4, 2009 Share Posted April 4, 2009 abcdefghijklmnopqrstuvwxyzaaabacadaeafagahaiajakalamanaoapaqarasatauavawaxayazbabbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzcacbcccdcecfcgchcicjckclcmcncocpcqcrcsctcucv That's the output of: <?php $str = 'a'; for ($i=0;$i<10;$i++){ echo $str++; } ?> The increment and decrement operators actually go from y to z to aa to ab, etc. So technically, we can rewrite this as: <?php $str = 'a'; for ($i=1;$i<101;$i++){ echo ($str == 'aa') ? ($str = 'a') : $str++; } ?> Now it works as desired. Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800858 Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 Or this... for($i=65;$i echo chr($i); Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800862 Share on other sites More sharing options...
Salkcin Posted April 4, 2009 Share Posted April 4, 2009 or this: $str = "fghijklmnopqrstuvwxyz"; for($i = 0; $i < strlen($str); $i++) { echo $str[$i] . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800869 Share on other sites More sharing options...
DarkWater Posted April 4, 2009 Share Posted April 4, 2009 Or this... for($i=65;$i<=90;$i++) echo chr($i); That won't loop back if he needs more though. Just saying. Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800871 Share on other sites More sharing options...
phpdragon Posted April 4, 2009 Author Share Posted April 4, 2009 I really appreciate the help, this is what I am trying to loop each time echo "<td bgcolor='#FF9999' width='150'><strong>=SUM(".$letter."5:".$letter.$restot.")</strong></td>"; and this would output the following loop 1 <td>=SUM(F5:F30)</td> loop 2 <td>=SUM(G5:G30)</td> loop 3 <td>=SUM(H5:H30)</td> loop 4 etc etc etc where the number of loops is equal to a variable called $coladd which is the number of extra columns needed to display the query results I dont want to loop all the letters in 1 row, moving on to double letters afterwards is fine as this is generated for a excel table and that is the natural naming schema for excel columns. Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800879 Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 Something like: $loop = 10; for($i=65;$i{ $i = ($i==91) ? 65 : $i; echo "=SUM(".chr($i)."5:".$letter.$restot.")"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800891 Share on other sites More sharing options...
phpdragon Posted April 4, 2009 Author Share Posted April 4, 2009 this gives the correct number of columns but it starts the letters from A $loop=$coladd; for($i=65;$i<=(64+$loop);++$i) { $i = ($i==91) ? 65 : $i; echo "<td bgcolor='#FF9999' width='150'><strong>=SUM(".chr($i)."5:".chr($i).$restot.")</strong></td>"; } how does this code work will it to continue to AA, AB, AC etc if needed? Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800906 Share on other sites More sharing options...
phpdragon Posted April 4, 2009 Author Share Posted April 4, 2009 Ok I have got the foolowing to work as I want it to <?php $loop=$coladd; for($i=70;$i<=(69+$loop);++$i) { $i = ($i==91) ? 70 : $i; echo "<td bgcolor='#FF9999' width='150'><strong>=SUM(".chr($i)."5:".chr($i).$restot.")</strong></td>"; } ?> I see that it is ascii code what happens when it gets to Z will it progress to AA etc or stop? I would like to be able to make the same fuction continue on to AA, AB, AC etc Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800926 Share on other sites More sharing options...
phpdragon Posted April 4, 2009 Author Share Posted April 4, 2009 This solved the problem works great thanks guys for your help <?php $str='F'; $loop=$coladd; for ($i=0;$i<$loop;$i++){ $letter=$str++; echo "<td bgcolor='#FF9999' width='150'><strong>=SUM(".$letter."5:".$letter.$restot.")</strong></td>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/#findComment-800932 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.