1

I have this table:

enter image description here

And time ago I was looking the way to make it possible display columns in rows with PHP table from MySQL data and I found it! Someone here help me with that.

But now I see a mistake, error in some line because I need to show the "prov_name" values from DB in the <th> not the field name from the table , I mean like this Fiddle.

Now I see that the error is in this line:

<th colspan="2"><?php echo "prov_name $x"; ?></th>

I know that is wrong because should be say $prov_name or something with variable because the prov_name always is different.

This is the code (all works fine except that):

$db = JFactory::getDbo();    
$query = $db->getQuery(true);
$data = array();
$query="SELECT * from provprices WHERE CA_id= ".$CA_id;
$results = mysql_query($query);

$db->setQuery("SELECT * from provprices");
while($row = mysql_fetch_array($results)) {
    $data[$row['prov_name']][] = $row;
}

$keys = array_keys($data);
$size = count($keys);
$vals = array();
// grouping:
// if there are six (cam1 to cam6)
// then group them by cam1, ... to cam6, then repeat until theres no more left
while(count($data) > 0) {
    foreach($keys as $key) {
        if(!empty($data[$key])) {
            $vals[] = array_shift($data[$key]);
        } else {
            unset($data[$key]); // remove them if empty
        }
    }
}
$vals = array_chunk($vals, $size); // split them by how many prov_names
?>
<div class="datagrid" id='Three'>
<table class="prices" >
    <!-- PROV NAMES -->
    <tr><?php for($x = 1; $x <= $size; $x++): ?>

////////////////////////////////////// THIS LINE
        <th colspan="2"><?php echo "prov_name $x"; ?></th>
////////////////////////////////////// THIS LINE

    <?php endfor; ?></tr>

    <!-- unitval totvals -->
    <tr><?php for($x = 1; $x <= $size; $x++): ?>
<td style="background-color: #00CDB9; border-bottom: 4px solid #006f7d; color: white;  font-weight: bold; border-right: 4px solid #006f7d;">Valor Unitario
</td>
<td style="background-color: #00CDB9; border-bottom: 4px solid #006f7d; color: white;  font-weight: bold; ">
Valor Total
</td>
    <?php endfor; ?>
    </tr>

    <!-- the grouped values -->
    <?php foreach($vals as $val): ?>
        <tr>
        <?php foreach($val as $v): ?>
            <td><?php echo $v['unitval']; ?></td>
            <td><?php echo $v['totval']; ?></td>
        <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>
5
  • hi. i remember this question. i think i was the one who answered this. why did you change it to mysql? i answered this with pdo lol. i think i understand now. instead of literal prov_name. you want to print the corresponding value to that prov_name. is that correct? if yes. where do you get them? maybe you need another query Commented Oct 25, 2014 at 1:41
  • @Ghost ! Yeah you're the only one that help me in that question. Because PDO don't works in my server, I don't know why. I show this in joomla, so when i change it works like a charm, except this part. Yes the value for that prov_name like the first fiddle. I get them from the same table(image), the first one is CAM1, the second one is CAM2..etc.. until CAM6 but not always are 6 prov_name's sometimes are just 2 or 3. After someone insert the info fiddle someone see the info with this. Commented Oct 25, 2014 at 2:45
  • in your fiddle. i can see camii, cam, etc. is this in the same table? what is the name of this column? Commented Oct 25, 2014 at 3:06
  • Yeah, is an example because always the prov_name is different. The column name is prov_name, so i the query should be Select prov_name from provprices(table name) where CA_id = ".$CA_id; and your magic.. Thanks @Ghost, I Hope you can help me. Commented Oct 25, 2014 at 3:22
  • 1
    so this is in the same table? we already assigned it in the keys you can use it Commented Oct 25, 2014 at 3:25

1 Answer 1

1

You're already close, just print them directly:

<!-- PROV NAMES -->
<tr>
<?php foreach($keys as $prov_name): ?>
    <th><?php echo $prov_name; ?></th>
<?php endforeach; ?>
</tr>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.