Jump to content

[SOLVED] display by group..


abs0lut

Recommended Posts

 

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";

}

?>

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..

 

 

 

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>';

 

 

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

<?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...

 

 

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>';
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.