Jump to content

Recommended Posts

Hi guys, I am trying to display my results as a table, my code is below..

 


$query1 = "SELECT * FROM Job_ID WHERE Job_Name ='$_SESSION[Job_Name]'";

  $result2 = mysql_query($query1)
       or die ("Couldn't execute query.");

while ($row2=mysql_fetch_array($result2)){

  		$Job_ID1=$row2['Job_ID'];
		}


	////
$sql2="SELECT * FROM ID_Table WHERE Job_ID IN (SELECT Job_ID FROM Job_ID WHERE Job_ID=$Job_ID1)";
   $result3 = mysql_query($sql2)
           or die ("Couldn't execute query.");
   
    while ($row3=mysql_fetch_array($result3)){
   
            $Skill_Name[]=$row3['Skill_Name'];
            $Weight[]=$row3['Weight'];
          }
$sep = '';
$sid_list = '';
foreach ($Skill_Name as $sid) {
  $sid_list .= "$sep$sid";
  $sep = ','; 
}
$sid_list .= '';


  

{
$sep1='';
foreach ($Weight as $wei){
$sid_list1 .= "$sep1$wei";
$sep1=',';

}
$sid_list1 .= '';
}

?>

 

I would like to print the contents of $sid_list and $sid_list1 as a table.  Is this possible?

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/
Share on other sites

First off, you are reinventing the wheel with some of your code.

 

This

$sep = '';
$sid_list = '';

foreach ($Skill_Name as $sid)
{
  $sid_list .= "$sep$sid";
  $sep = ','; 
}
$sid_list .= '';

Could be done with just this

$sid_list = implode(',', $Skill_Name);

 

Also, your queries aren't making sense to me. The first query gets a singe JOB_ID (or if it get's multiple you are left with only one assigned to the $Job_ID1 valiable). Then int he second query you do a subquery in the WHERE clause for this

Job_ID IN (SELECT Job_ID FROM Job_ID WHERE Job_ID=$Job_ID1)

But, you are just creating a more complex way of testing if the value is equal to another SINGLE value. Besides you can get the restults you want with a single query (if I am reading that code right)

 

Try this:

$query="SELECT `Skill_Name`, `Weight`
        FROM ID_Table
        JOIN Job_ID ON ID_Table.Job_ID = Job_ID.Job_ID
        WHERE Job_ID.Job_Name ='{$_SESSION[Job_Name]}'";

$result = mysql_query($query) or die ("Couldn't execute query.");

while ($row=mysql_fetch_array($result))
{
    $skillTbl .= "<tr><td>{$row['Skill_Name']}</td></tr>\n";
    $WeightTbl .= "<tr><td>{$row['Weight']}</td></tr>\n";
}

echo "SKILLS:<br>\n"
echo "<table>\n{$skillTbl}<table>\n";

echo "WEIGHTS:<br>\n";
echo "<table>\n{$WeightTbl}<table>\n";

 

Thank you for helping me clear my query up, I am new to programming and really appreciate your advise.  That code works fine, and produces the two tables.  But what i am trying to do is create a table with 2 coloums - Skill and Weight.  I tried this code:

 

$query="SELECT `Skill_Name`, `Weight`
        FROM ID_Table
        JOIN Job_ID ON ID_Table.Job_ID = Job_ID.Job_ID
        WHERE Job_ID.Job_Name ='{$_SESSION[Job_Name]}'";

$result = mysql_query($query) or die ("Couldn't execute query.");

while ($row=mysql_fetch_array($result))
{
    $skillTbl .= "<tr><td>{$row['Skill_Name']}</td></tr>\n";
    $WeightTbl .= "<tr><td>{$row['Weight']}</td></tr>\n";
}

echo"<table width='200' border='1'>
<tr>
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td>{$skillTb1}</td>
</tr>
<tr>
<td>{$WeightTb1}</td>
</tr>
</table>";
?>

 

But it doesnt output anything.

...what i am trying to do is create a table with 2 coloums - Skill and Weight.

 

A simple modification will take care of that

 

<?php

$query="SELECT `Skill_Name`, `Weight`
        FROM ID_Table
        JOIN Job_ID ON ID_Table.Job_ID = Job_ID.Job_ID
        WHERE Job_ID.Job_Name ='{$_SESSION[Job_Name]}'";

$result = mysql_query($query) or die ("Couldn't execute query.");

while ($row=mysql_fetch_array($result))
{
    $tblRows .= "<tr>";
    $tblRows .= "<td>{$row['Skill_Name']}</td>";
    $tblRows .= "<td>{$row['Weight']}</td>";
    $tblRows .= "</tr>\n";
}

echo "<table width=\"200\" border=\"1\">\n";
echo "<tr><th>Skill<th><th>Weight</th></tr>\n";
echo $tblRows;
echo "<table>\n";

?>

silly mistake...thanks for that.

 

Iv got the data showing now, but I still cant display the info as a table with two coloums. 

 

The output the following code produces is:

 

Column 1  Column 2

C

C#

Java

4

5

2

 

But what i want is:

 

Column 1  Column 2

C                4

C#              5

Java            2

 

My code:

 

$query="SELECT `Skill_Name`, `Weight`
        FROM ID_Table
        JOIN Job_ID ON ID_Table.Job_ID = Job_ID.Job_ID
        WHERE Job_ID.Job_Name ='{$_SESSION[Job_Name]}'";

$result = mysql_query($query) or die ("Couldn't execute query.");

while ($row=mysql_fetch_array($result))
{
    $skillTbl .= "<tr><td>{$row['Skill_Name']}</td></tr>\n";
    $WeightTbl .= "<tr><td>{$row['Weight']}</td></tr>\n";
}

echo"<table width='200' border='1'>
<tr>
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td>{$skillTbl}</td>
<td>{$WeightTbl}</td>
</tr>
</table>";
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.