Jump to content

[SOLVED] increment a letter in a loop


phpdragon

Recommended Posts

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>";
}

 

Link to comment
https://forums.phpfreaks.com/topic/152439-solved-increment-a-letter-in-a-loop/
Share on other sites

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;
}

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>";}
}
?>

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

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.

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.

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?

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

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>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.