AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Off-Topic (https://forums.alliedmods.net/forumdisplay.php?f=15)
-   -   Mysql columns in a loop (https://forums.alliedmods.net/showthread.php?t=262212)

bowisen 04-30-2015 05:51

Mysql columns in a loop
 
Let's say I have a Mysql table with the columns year_2012, year_2013 and year_2014. I select all columns in the table

$fraga = "SELECT * FROM TABLE WHERE medlemsnr=12 LIMIT 1";
$svar = mysqli_query($conn,$fraga);
$row = mysqli_fetch_array($svar);
extract ($row);

If I want to display the value of year_2013 it works with 'echo $year_2013'.

My problem is that I want to make a loop to display all three columns. I have tried this but it doesn't work:

for ($theYear=2012;theYear<2015;theYear++) {
echo $year_$theYear;
}

Is there a solution?

DarkDeviL 04-30-2015 11:34

Re: Mysql columns in a loop
 
Quote:

Originally Posted by bowisen (Post 2291760)
Let's say I have a Mysql table with the columns year_2012, year_2013 and year_2014. I select all columns in the table

$fraga = "SELECT * FROM TABLE WHERE medlemsnr=12 LIMIT 1";
$svar = mysqli_query($conn,$fraga);
$row = mysqli_fetch_array($svar);
extract ($row);

If I want to display the value of year_2013 it works with 'echo $year_2013'.

My problem is that I want to make a loop to display all three columns. I have tried this but it doesn't work:

for ($theYear=2012;theYear<2015;theYear++) {
echo $year_$theYear;
}

Is there a solution?

Could you explain what you are trying to do with the information? What's your final goal?

Your code is PHP, and doesn't seem to be related to SourceMod scripting at all.

BTW; in the future, then post your code into [CODE] tags.

Exolent[jNr] 04-30-2015 11:46

Re: Mysql columns in a loop
 
This is Scripting Help for SourceMod scripting, but to answer your question:

PHP Code:

echo ${'year_' $theYear}; 


Neeeeeeeeeel.- 04-30-2015 13:10

Re: Mysql columns in a loop
 
This will work too
PHP Code:

foreach ($row as $key=>$value){
    if (
preg_match("/^year_/"$key)){
        echo 
$value;
    }




All times are GMT -4. The time now is 01:17.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.