Jump to content

Number array


Mutley

Recommended Posts

I receive lots of:

1

1

1

 

I need it to display the $points with a new variable for it, so it would show as:

 

$newvar1 = $points;

$newvar2 = $points;

$newvar3 = $points;

$newvar4 = $points;

 

But with a loop it would obvioulsy just be 1 of the above but I can't assign variables to it (as it would just loop the same number/variable).

Link to comment
https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210237
Share on other sites

Then fert's earlier suggestion should work - read the link he provided

<?php
$count=1;
while (list($points) = mysql_fetch_row($res)) {
    $var = 'newvar'.$count;
    $$var = $points;
    ++$count;
}

echo $newvar1. '<br>';
echo $newvar2. '<br>';
echo $newvar3. '<br>';
echo $newvar4. '<br>';
echo $newvar5. '<br>';
echo $newvar6. '<br>';

// if you had a 1000, would you still insist on this method?
?>

Link to comment
https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210253
Share on other sites

I got this to work:

 

<?php

$count=0;
$p1_slot = 'p1_slot';
$sql = "SELECT u.user_id, u.slot, p.name, p.points
            FROM loadout u
                INNER JOIN products p ON u.prod_id = p.prod_id WHERE u.user_id = '1'
            ORDER BY u.user_id, u.slot";
            
    $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
    while (list($user, $slot, $prod, $points) = mysql_fetch_row($res)) {
        echo "$$p1_slot$count = $points<br>";
	$count++;
    }

?>

 

$p1_slot0 = 3

$p1_slot1 = 5

$p1_slot2 = 4

$p1_slot3 = 4

$p1_slot4 = 10

$p1_slot5 = 3

 

But if I wanted to use these variables in a part of the script further on, how would I do that? As at the moment they don't show in the code, only when echoed.

Link to comment
https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210401
Share on other sites

<?php

$count=0;
$p1_slot = 'p1_slot';
$sql = "SELECT u.user_id, u.slot, p.name, p.points
            FROM loadout u
                INNER JOIN products p ON u.prod_id = p.prod_id WHERE u.user_id = '1'
            ORDER BY u.user_id, u.slot";
            
    $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
    while (list($user, $slot, $prod, $points) = mysql_fetch_row($res)) {
          $newVar = "p1_slot" . $count;
          $$newVar = $points<br>";
	$count++;
    }

?>

 

I thought barand pointed this out already, but here it is for a second time.

Link to comment
https://forums.phpfreaks.com/topic/43294-number-array/#findComment-210506
Share on other sites

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.