nishmgopal Posted March 16, 2009 Share Posted March 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/ Share on other sites More sharing options...
Maq Posted March 16, 2009 Share Posted March 16, 2009 Yes, have you attempted putting it in a table? Start your table before the loop, and put tags around your values inside the loop. There are many examples on phpfreaks, this is a common practice. Quote Link to comment https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/#findComment-785909 Share on other sites More sharing options...
nishmgopal Posted March 16, 2009 Author Share Posted March 16, 2009 so you mean something like this: echo "table width='200' border='1'>"; $sep = ''; $sid_list = ''; foreach ($Skill_Name as $sid) { $sid_list .= "$sep$sid"; $sep = ','; } <tr>$sid_list .= ''</tr>; echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/#findComment-785911 Share on other sites More sharing options...
Psycho Posted March 16, 2009 Share Posted March 16, 2009 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"; Quote Link to comment https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/#findComment-785930 Share on other sites More sharing options...
nishmgopal Posted March 16, 2009 Author Share Posted March 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/#findComment-785947 Share on other sites More sharing options...
Maq Posted March 16, 2009 Share Posted March 16, 2009 You're using "$skillTb1" instead of "$skillTbl". (notice the 'L' NOT the number '1') Quote Link to comment https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/#findComment-785953 Share on other sites More sharing options...
Psycho Posted March 16, 2009 Share Posted March 16, 2009 ...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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/#findComment-785963 Share on other sites More sharing options...
nishmgopal Posted March 16, 2009 Author Share Posted March 16, 2009 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/#findComment-785965 Share on other sites More sharing options...
nishmgopal Posted March 16, 2009 Author Share Posted March 16, 2009 guys thank you so much for your help. I managed to get the data displayed correctly. Quote Link to comment https://forums.phpfreaks.com/topic/149670-solved-displaying-results-of-an-array/#findComment-785974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.