Ujj Posted August 10, 2012 Share Posted August 10, 2012 hello phpfreaks, any idea on how to assign value to a auto increment variable inside a loop .lets say for example $letter="a"; for($i=1;$i<=5;$i++){ $letter = $i; $letter++; } i want output in this way; $a=1 $b=2 $c=3 $d=4 $e=5 or any alternative to this? Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/ Share on other sites More sharing options...
MMDE Posted August 10, 2012 Share Posted August 10, 2012 I think you need to get the logic a bit more straight. Do you really want to output this?: $a=1 $b=2 $c=3 $d=4 $e=5 Something I scribbled down in a hurry: <?php for($i=1; $i<6; $i++){ echo '$'.chr($i+96).'='.$i."\n"; } ?> even though I doubt this is what you want lol. I bet you should be looking into arrays or just use the function chr. Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368324 Share on other sites More sharing options...
Barand Posted August 10, 2012 Share Posted August 10, 2012 if you want to create those 5 variables and assign them values 1 to 5 <?php $letter="a"; for($i=1;$i<=5;$i++){ $$letter = $i; $letter++; } echo "$a, $b, $c, $d, $e"; // --> 1, 2, 3, 4, 5 ?> Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368330 Share on other sites More sharing options...
Ujj Posted August 10, 2012 Author Share Posted August 10, 2012 I think you need to get the logic a bit more straight. Do you really want to output this?: $a=1 $b=2 $c=3 $d=4 $e=5 Something I scribbled down in a hurry: <?php for($i=1; $i<6; $i++){ echo '$'.chr($i+96).'='.$i."\n"; } ?> even though I doubt this is what you want lol. I bet you should be looking into arrays or just use the function chr. hi MMDE, thanks for the reply.may be the way i asked my question was not right the solution Barand provided is what i was looking for. thank you both of you for quick reply and extra thanks to Barand for providing me the solution. Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368333 Share on other sites More sharing options...
Ujj Posted August 10, 2012 Author Share Posted August 10, 2012 let me now extend the issue a little bit more this is what i am trying to to to draw a graph, $letter = "a"; foreach($getItem as $key=>$value){ $sql = "select itemID,title,priceHistory, updateHistory from item_detail where itemID =1010 and groupID=6"; $rs = mysql_query($sql) or die(mysql_error()); $rs = mysql_fetch_row($rs); $header.= " '$rs[1]($rs[0])', "; $upDate = explode(',',$rs[3]); // $rs[3]=1,2,3,4,5 $$letter = explode(',',$rs[2]); // $rs[2] = 1,2,3,4,5 $letter++; } lets say the above loop runs for 4 times.how can i loop to print like below. $key=0; for(length of $letter){ echo "['$upDate[$key++]', $a[$key++]', '$b[$key++]' ,$c[$key++]', $d[$key++]'........]"; } where the alphabetic variables can goes up to any level obviously up to z Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368336 Share on other sites More sharing options...
Barand Posted August 10, 2012 Share Posted August 10, 2012 Instead of creating variables, why don't you do as MMDE suggested and use arrays? It's a better way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368338 Share on other sites More sharing options...
Ujj Posted August 10, 2012 Author Share Posted August 10, 2012 Instead of creating variables, why don't you do as MMDE suggested and use arrays? It's a better way to do it. could you give me idea how to do it please Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368349 Share on other sites More sharing options...
Christian F. Posted August 10, 2012 Share Posted August 10, 2012 http://no2.php.net/manual/en/language.types.array.php Edit: Also, what Jazzman1 said below. Almost missed that one, Jazzman1. Thanks for picking it up. Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368358 Share on other sites More sharing options...
jazzman1 Posted August 10, 2012 Share Posted August 10, 2012 No, no, NEVER EVER run queries in loops. Take a look at here -> http://forums.phpfreaks.com/index.php?topic=363573.0 Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368359 Share on other sites More sharing options...
Ujj Posted August 10, 2012 Author Share Posted August 10, 2012 No, no, NEVER EVER run queries in loops. Take a look at here -> http://forums.phpfreaks.com/index.php?topic=363573.0 thank you jazzman1 and ChristianF can you suggest me how can i avoid query inside loop in this condition below pleaseeeeeeeee,i can't find the way around $sql = "select user_item,track_item,g_id from tracked_item as t left join item_detail on t.user_id=itemID where t.user_id=$userid order by itemSold desc limit 1 "; $rs = mysql_query($sql); $row = mysql_fetch_row($rs); $gid=$row[2]; $items= $row[0].','.$row[1]; $getItem = explode(',',$items); $letter = "a"; foreach($getItem as $key=>$value){ $sql = "select itemID,title,priceHistory,itemSoldHistory,updateHistory from item_detail where itemID =$value and groupID=$gid"; $rs = mysql_query($sql) or die(mysql_error()); $rs = mysql_fetch_row($rs); $header.= " '$rs[1]($rs[0])', "; $upDate = explode(',',$rs[4]); $$letter = explode(',',$rs[2]); $letter++; } Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368367 Share on other sites More sharing options...
Christian F. Posted August 10, 2012 Share Posted August 10, 2012 First off: Please use the [code][/code] tags when posting code. It helps make both your code and your post a lot easier to read. Secondly, what you're looking for is JOINs. Since you're already using a LEFT JOIN in the first query, all you need to do is to add the fields and table from the second query to it. Using its own JOIN statement, of course. Plenty of tutorials and guides online, if you don't know how to use JOINs in SQL queries. Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368376 Share on other sites More sharing options...
Ujj Posted August 10, 2012 Author Share Posted August 10, 2012 First off: Please use the [code][/code] tags when posting code. It helps make both your code and your post a lot easier to read. Secondly, what you're looking for is JOINs. Since you're already using a LEFT JOIN in the first query, all you need to do is to add the fields and table from the second query to it. Using its own JOIN statement, of course. Plenty of tutorials and guides online, if you don't know how to use JOINs in SQL queries. sorry about that as this is my first post i did not realize the use of to use left join on my second query i am using the out come of this query $sql = "select user_item,track_item,g_id from tracked_item as t left join item_detail on t.user_id=itemID where t.user_id=$userid order by itemSold desc limit 1 "; so how can I use the value pulled by this query using join please suggestion Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368390 Share on other sites More sharing options...
jazzman1 Posted August 10, 2012 Share Posted August 10, 2012 Assuming that you have 8 users and their id's are form 1 - 8. In this case, you can use SQL IN operator and php implode function. $display_user = array( 1 => 'jazzman', 2 => 'Drummin', 3 => 'mbb87', 4 => 'CristianF', 5 => 'jazzman1', 6 => 'Barand', 7 => 'NoBBy', 8 => 'Ujj' ); $ids = implode(', ', array_keys($display_user)); $sql = "select user_item,track_item,g_id from tracked_item as t left join item_detail on t.user_id=itemID where t.user_id IN ($ids) order by itemSold desc limit 1 "; Quote Link to comment https://forums.phpfreaks.com/topic/266901-assign-value-to-auto-increment-variable/#findComment-1368436 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.