1

Hey guys I need your help, I have this table

enter image description here

and I want to shows like this FIDDLE, really I don't know how to do this because sometimes just exist two columns(prov_name ) and sometimes exist more that two rows please help me if you can !

Hope you understand me. Thanks so much !

In this way I can be able to select data from Joomla.

    $db =& JFactory::getDBO();
    $query = 'SELECT prov_name FROM provprices where CA_id = '.$CA_id;
    $db->setQuery($query);
    $result = $db->loadObjectList();
    $prov_name = $result[0];
    echo $prov_name->prov_name;
2
  • sidenote: why does CA_id entries on your table share the same key? is that supposed to be a primary key? Commented Aug 4, 2014 at 2:04
  • @Ghost isn't a primary key, is a foreign key. Hope you can help me. Thanks Commented Aug 4, 2014 at 2:58

2 Answers 2

1

First off, in order for your data to be presented like that obviously it must be grouped accordingly.

The first row is, the prov_name's, so you can use GROUP BY or you cal also do it in PHP. Based of the sample data, it should have from 1 to 6.

Then the second row is just a simple unitval and totval according to how many prov_name's.

Third is the and the rest is the grouping of the values. See Example:

$db = new PDO('mysql:host=localhost;dbname=DATABASE_NAME;charset=utf8', 'USERNAME', 'PASSWORD');
$data = array();
$results = $db->query("SELECT * from YOUR_TABLE_NAME");
while($row = $results->fetch(PDO::FETCH_ASSOC)) {
    $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

?>

<table border="1" cellpadding="10">
    <!-- PROV NAMES -->
    <tr><?php for($x = 1; $x <= $size; $x++): ?>
        <th colspan="2"><?php echo "prov_name $x"; ?></th>
    <?php endfor; ?></tr>

    <!-- unitval totvals -->
    <tr><?php for($x = 1; $x <= $size; $x++): ?>
        <td>unitval</td><td>totval</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>
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks so much for your answer, I replace the data with mine and then I put this code in an apart file in Joomla and don't works, the page loads in white, I think is because the first 5 lines isn't like joomla select their data from DB. I edited my code with the way that I use to connect to Joomla DB, hope helps !! I appreciate so much your help and time with this case.
i haven't used joomla yet but should be the same, the important thing is, first, just transpose how joomla selects and just do the same concept, in the end you just need to arrive on the same $data contents, by the way the example used is PDO, i don't know why but should be working
Awesome men ! I tried your code in localhost and works like a charm ! I will find the way to transform that lines in Joomla lines because don't works like that in Joomla I don't know why. I appreciate so much your help.
@user3810795 yes ofcourse i tried it before posting it in here :).. i haven't used joomla before, but as my comment before, you just need to transform it into select in joomla. glad this answer guided you
You noticed that the prov_name field in table isn't the same like the MySQL DB, is prov_name 1, prov_name 2, prov_name 3.. Sorry hope you can help me!
|
0

I think you must do this first,

try looping to show prov_name in horizontal,

and then you fetch again in query with

"SELECT * FROM table_test where prov_name = '$prov_name'"

in Variable $prov_name you must have a value with CAM2 or CAM3 .etc

sorry just a logic :)

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.