Raised This Month: $12 Target: $400
 3% 

Mysql columns in a loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
bowisen
New Member
Join Date: Apr 2015
Old 04-30-2015 , 05:51   Mysql columns in a loop
Reply With Quote #1

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?
bowisen is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 04-30-2015 , 11:34   Re: Mysql columns in a loop
Reply With Quote #2

Quote:
Originally Posted by bowisen View Post
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.
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread].
DarkDeviL is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-30-2015 , 11:46   Re: Mysql columns in a loop
Reply With Quote #3

This is Scripting Help for SourceMod scripting, but to answer your question:

PHP Code:
echo ${'year_' $theYear}; 
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Neeeeeeeeeel.-
Some Guy Yellin'
Join Date: Jul 2010
Location: Argentina
Old 04-30-2015 , 13:10   Re: Mysql columns in a loop
Reply With Quote #4

This will work too
PHP Code:
foreach ($row as $key=>$value){
    if (
preg_match("/^year_/"$key)){
        echo 
$value;
    }

__________________
Neeeeeeeeeel.- is offline
Send a message via Skype™ to Neeeeeeeeeel.-
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:42.


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