1

hey guys is this even possible to be done in php

<?php   
$sectorcount = $row_SectorCount['COUNT(*)'];
//number of rows in database
for ($i = 1; $i <= $sectorcount; $i++)
{
${spendingname . $i}= array();
${spendingpercent . $i} = array();
${spendingid . $i} = array();

mysql_select_db($database_conn2, $conn2);
$query_Spending = "SELECT CONCAT(spending.SectorID, spending.ExpenditureID) AS 'SpendingID',
expenditure.ExpenditureName, spending.SpendingPercent, spending.SectorID
FROM spending   
INNER JOIN expenditure ON spending.ExpenditureID = expenditure.ExpenditureID
WHERE spending.SectorID = ".$i;
$Spending = mysql_query($query_Spending, $conn2) or die(mysql_error());
$totalRows_Spending = mysql_num_rows($Spending);
while($row_Spending = mysql_fetch_assoc($Spending))
{
${spendingname.$i}[] = $row_Spending['ExpenditureName'];
${spendingpercent.$i}[] = $row_Spending['SpendingPercent'];
${spendingid.$i}[]= $row_Spending['SpendingID'];
}
mysql_free_result($Spending);
}

i was planning to use this here in this context. having an array to draw out the values using a where clause and display them individually

0

5 Answers 5

3

This is what you are looking for:

for ($i = 0; $i < 14; $i++) {
    ${'name' . $i} = $i;
}

echo $name3;

But you should just use an array:

$name = array();
for ($i = 0; $i < 14; $i++) {
    $name[$i] = $i;
}

echo $name[3];

If you need an array in an array, go ahead and do that too. If you need more nested arrays than that, you should probably be using a class somehow instead of all that array nesting.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you!! i want to try it on an array within an array like $name.$i = array (); then using database to draw the data out
@JervisChionh You should upvote answers you find useful. You should also accept an answer to your question by clicking on the check mark. Welcome to StackOverflow.
i have in sufficient rep ! i would love to do that
heyy levi what if i want something like that $spendingname = array(); for($i=1; $i<=13; $i++) { $spendingname[$i] = one,two,three; } echo $spendingid[1]; ?>
@JervisChionh I'm confused by your code. You use both $spendingname and $spendingid.
|
2

First off, I second the suggestion to use an array instead. Variable variables are not meant to be used like this.

That said, it is possible by accessing ${$name.$i}, or perhaps doing something like

$varname = $name.$i;
$$varname = 1; // or read from $$varname

Comments

1

Use an array. That's what they're there for.

3 Comments

what if i want to have an array within an array?
Then just do so. Nothing wrong with arrays in arrays in arrays in arrays in arrays.
in arrays in arrays. They are called nested arrays.
1

As Kolink said, that is what an array is for!

However, What you are asking can be achieved:

Read More Here

Basically,

<?php

for($i=0; $i<=13; $i++){

    $varname = "name" . $i;
    $$varname=$i;

}

echo $name1; // 1

?>

Comments

1

It will not give desired output 1 instead will give 13

What s the reason of doing like this. you can make array a access is later on use

 for($i=0; $i<=13; $i++)
 {
        $name[$i]=$i;
 }

      echo $name[1];                    *//it will echo 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.