Jump to content

[SOLVED] trying to populate list of variables from an array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
tingting

tingting

    Newbie

  • Members
  • Pip
  • 2 posts
here's the code I've got, where position1 through position14 are mysql table rows. Why isn't this working?


$i=1;

while ( $i < 14 ){

    if ($row[position$i] >= 0){

        ${position$i}=$row[position$i];

        ${story$i}="news";

    }elseif($row2[position$i] >= 0){

        ${position$i}=$row2[position$i];

        ${story$i}="pages";

    }

    echo ${story$i};

    $i++;

}


Edited by tingting, 04 June 2010 - 05:16 PM.


#2
tingting

tingting

    Newbie

  • Members
  • Pip
  • 2 posts
I figured it out!, with a variation :D

method 1:
$i=1;
while ( $i < 14 ){
    if ($row["position$i"] > 0){
        ${"position$i"}=$row["position$i"];
        ${"story$i"}="news";
    }elseif($row2["position$i"] > 0){
        ${"position$i"}=$row2["position$i"];
        ${"story$i"}="pages";
    }
    echo ${"position$i"};
    echo "<br />";
    $i++;
}  

method 2:
$i=1;
while ( $i < 14 ){
    if ($row["position$i"] > 0){
        $position[$i]=$row["position$i"];
        $story[$i]="news";
    }elseif($row2["position$i"] > 0){
        $position[$i]=$row2["position$i"];
        $story[$i]="pages";
    }
    echo $position[$i];
    echo "<br />";
    $i++;
}