How to create variables on the fly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

How to create variables on the fly

Post by nishmgopal »

Hi guys

I am making a system where jobs are matched to people skills. At the moment my system is only able to display information that I specify in the code, for example:

if in the code I have:

$skill1=$row['C#];
$skill2=$row['C++];

I will only be able to display C# and c++. But if at some point another skills was added to the database, how can I get that third skills to be assigned to $skill3 without having to go modify the code...is this even possible? SO basically, right now my system is specific to a set number of skills, what do i need to do to make it a general system?

Thanks
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: How to create variables on the fly

Post by susrisha »

use an array for $skill instead

and assining for the elements can be done using foreach statement

Code: Select all

 
foreach($row as $row_key=>$row_value)
    {
        $skill[$i]=$row_value;
                $i++;
    }
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: How to create variables on the fly

Post by nishmgopal »

How would I then be able to display this information in a table format. right now I can define the row by say:


<td>$skill1</td>

and as i no there are 6 skills i no i need 6 rows. But what if I have n amount of skills? How can i define n amount of rows?

is this even possible?
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: How to create variables on the fly

Post by susrisha »

Code: Select all

 
for($i=0;$i<count($skills);$i++)
{
    echo "<td>";
    echo $skills[$i];
    echo "</td>";
}
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: How to create variables on the fly

Post by nishmgopal »

Thank you, I will try this later on and let you know how I get on.

thanks
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: How to create variables on the fly

Post by nishmgopal »

this is my code and it keeps returning a blank screen?

Code: Select all

 
$query = "SELECT * FROM Persons";
   $result = mysql_query($query);
   
 
  for($i=0;$i<count($skills);$i++)
 {
    echo "<td>";
    echo $skills[$i];     echo "</td>"; }
 
 
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: How to create variables on the fly

Post by nishmgopal »

I have also tried this code:

Code: Select all

$query = "SELECT * FROM Persons";
   $result = mysql_query($query);
   
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
 
array();
 
foreach($row as $row_key=>$row_value)
    {
        $skill[$i]=$row_value;
                $i++;
    }
 
    {
    
    echo $skill[$i];
    
    }
    }
But I still get a blank screen - no errors!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to create variables on the fly

Post by onion2k »

That's not how you access database content. Look at the PHP manual page for mysql_query().
Post Reply