How to create variables on the fly
Moderator: General Moderators
-
nishmgopal
- Forum Contributor
- Posts: 101
- Joined: Tue Mar 03, 2009 9:38 am
How to create variables on the fly
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
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
Re: How to create variables on the fly
use an array for $skill instead
and assining for the elements can be done using foreach statement
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
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?
<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?
Re: How to create variables on the fly
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
Thank you, I will try this later on and let you know how I get on.
thanks
thanks
-
nishmgopal
- Forum Contributor
- Posts: 101
- Joined: Tue Mar 03, 2009 9:38 am
Re: How to create variables on the fly
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
I have also tried this code:
But I still get a blank screen - no errors!
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];
}
}Re: How to create variables on the fly
That's not how you access database content. Look at the PHP manual page for mysql_query().