Page 1 of 1
How to create variables on the fly
Posted: Thu Mar 12, 2009 6:08 am
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
Re: How to create variables on the fly
Posted: Thu Mar 12, 2009 6:16 am
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++;
}
Re: How to create variables on the fly
Posted: Thu Mar 12, 2009 6:25 am
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?
Re: How to create variables on the fly
Posted: Thu Mar 12, 2009 6:27 am
by susrisha
Code: Select all
for($i=0;$i<count($skills);$i++)
{
echo "<td>";
echo $skills[$i];
echo "</td>";
}
Re: How to create variables on the fly
Posted: Thu Mar 12, 2009 6:30 am
by nishmgopal
Thank you, I will try this later on and let you know how I get on.
thanks
Re: How to create variables on the fly
Posted: Thu Mar 12, 2009 7:14 am
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>"; }
Re: How to create variables on the fly
Posted: Thu Mar 12, 2009 7:56 am
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!
Re: How to create variables on the fly
Posted: Thu Mar 12, 2009 7:57 am
by onion2k
That's not how you access database content. Look at the PHP manual page for mysql_query().