Jump to content

assign value to auto increment variable


Ujj

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :confused:

 

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.

Link to comment
Share on other sites

let me now extend the issue a little bit more  :D

 

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  :shy:

 

 

Link to comment
Share on other sites

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  :confused:

 

$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++;

}

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.