Jump to content

[SOLVED] display by group..


abs0lut

Recommended Posts

database structure

ctableiy5.th.jpg

 

it should display this

 

        userid1  userid2  userid3

iqid1  cost      cost      cost

iqid2  cost      cost      cost

iqid3  cost      cost      cost

 

please help me..

 

Link to comment
Share on other sites

 

You can read your costs into a 2D array then print that array as a table. For example

 

<?php

$result = mysql_query("SELECT iqid, cost, userid FROM my_table");

while ($row = mysql_fetch_array($result))

{

  $arr[$row['iqid']][$row['userid']] = $row['cost'];

  $uid[] = $row['userid'];

}

$uid = array_unique($uid);

$qid = array_keys($arr);

foreach($uid as $userid) echo "\t$userid";

echo "\n";foreach($qid as $iqid)

{

  echo "$iqid\t";

  foreach($uid as $userid)

  {

    if (isset($arr[$iqid][$userid])) echo $arr[$iqid][$userid];

    else echo "    ";

    echo "\t";

  }

  echo "\n";

}

?>

Link to comment
Share on other sites

file.php

<?php
$post=$_GET['post'];
$result = mysql_query("SELECT iqid, cost, userid FROM ctable WHERE postid=$post");
while ($row = mysql_fetch_array($result))
{
  $arr[$row['iqid']][$row['userid']] = $row['cost'];
  $uid[] = $row['userid'];
}
$uid = array_unique($uid);
$qid = array_keys($arr);
foreach($uid as $userid) echo "\t$userid";
echo "\n";foreach($qid as $iqid)
{
  echo "$iqid\t";
  foreach($uid as $userid)
  {
    if (isset($arr[$iqid][$userid])) echo $arr[$iqid][$userid];
    else echo "    ";
    echo "\t";
  }
  echo "\n";
}
?>

when I enter file.php?post=2

all iqids are selected

please help me..

 

 

Link to comment
Share on other sites

why I couldn't edit my previous posts?  I  already solved the above posts but I have problem with alignment of cost..

\n and \t is not functioning, userids are not aligned with the cost..

please change it with tables??

 

Link to comment
Share on other sites

 

Replace this part in your code:

 

echo '<table><tr><td></td>';
foreach($uid as $userid) echo "<td>$userid</td>";
echo '</tr>';
foreach($qid as $iqid)
{
  echo "<tr><td>$iqid</td>";
  foreach($uid as $userid)
  {
    echo '<td>';
    if (isset($arr[$iqid][$userid])) echo $arr[$iqid][$userid];
    echo '</td>';
  }
  echo "</tr>";
}
echo '</table>';

 

 

Link to comment
Share on other sites

echo '<table cellpadding=5><tr><td></td>';
foreach($uid as $userid) echo "<td>$userid</td>";
echo '</tr>';
foreach($qid as $iqid)
{
  echo "<tr><td>$iqid</td>";
  foreach($uid as $userid)
  {
    echo '<td>';
    if (isset($arr[$iqid][$userid])) echo $arr[$iqid][$userid]."<input type='checkbox' name='sa[]'>";
    echo '</td>';

  }
  echo "</tr>";
}
echo '</table>';

thank you very much..

I've added checkbox in every cost, I have problem with alignment again..

the checkbox is not aligned with each other vertically

Link to comment
Share on other sites

<?php
echo'<form method="post" name="myform" action="file.php">';
$result = mysql_query("SELECT iqid, cost, userid FROM ctable where postid=$post");
while ($row = mysql_fetch_array($result))
{
  $arr[$row['iqid']][$row['userid']] = $row['cost'];
  $uid[] = $row['userid'];
}
$uid = array_unique($uid);
$qid = array_keys($arr);
echo '<table cellpadding=5><tr><td></td>';
foreach($uid as $userid) echo "<td>$userid</td>";
echo '</tr>';
foreach($qid as $iqid)
{
	 echo "<tr><td>$iqid</td>";
  		foreach($uid as $userid)
  		{
    	echo '<td>';
    		if (isset($arr[$iqid][$userid])) echo $arr[$iqid][$userid];
	echo '</td>';
	}
  	}
$result = mysql_query("SELECT cid FROM ctable WHERE postid=$post");
while ($row = mysql_fetch_array($result))
{
echo "<input type='checkbox' name='sa[]' value='$row[cid]'>";
}
echo "</tr>";
echo '</table>';
echo '<input type="submit" name="listing" />';
echo'</form>';	

?>

I've added checkbox and I need to get the cid..

I have problem in positioning, the checkbox appeared in the top of the userid(above userid)

I want the checkbox to appear beside with the cost..

please help me...

 

Link to comment
Share on other sites

 

In this code, I added cid to the query as well as in a second array $arr2 then print 2 cells for each user that will have cost and checkbox. I hope this helps

 

<?php
$result = mysql_query("SELECT iqid, cost, userid, cid FROM ctable");
while ($row = mysql_fetch_array($result))
{
  $arr[$row['iqid']][$row['userid']]  = $row['cost'];
  $arr2[$row['iqid']][$row['userid']] = $row['cid'];
  $uid[] = $row['userid'];
}
$uid = array_unique($uid);
$qid = array_keys($arr);
echo '<table cellpadding=5><tr><td></td>';
foreach($uid as $userid) echo "<td colspan='2'>$userid</td>";
echo '</tr>';
foreach($qid as $iqid)
{
  echo "<tr><td>$iqid</td>";
  foreach($uid as $userid)
  {
    if (isset($arr[$iqid][$userid]))
    {
        echo "<td>".$arr[$iqid][$userid]."</td><td><input type='checkbox' name='sa[]' value='{$arr2[$iqid][$userid]}'></td>";
    }
    else echo '<td colspan='2'></td>';

  }
  echo "</tr>";
}
echo '</table>';
?>

 

Link to comment
Share on other sites

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.