Forums

Select from and array_combine

federico85 27 Jan, 2011
Hi guys...
I'm trying to combine the results of two arrays:

<?php
// first array
$points = array(100, 90, 80);
// second array
$db =& JFactory::getDBO();
$query = "
SELECT `cf_id`, 
FROM `#__chronoforms_my_table`
WHERE `check2` != '' AND `radio0` = 'M' AND `cat_cerchi` = 'D' AND `rifiuto_cerchi` = ''AND `edizione_anno` = '$anno_impostato'
ORDER BY somma_pts_cerchi DESC, date_int ASC
LIMIT 10
";
$db->setQuery($query);
$result = $db->loadObjectlist();
// combine the arrays
$result_2 = array_combine ($result, $points);

foreach($result_2 as $k=>$v){ echo $k.' -> '.$v.'<br />'; }
?>

But it doesn't work.
I'd like it to show me something like this:
first cf_id -> 100
second cf_id -> 90
third cf_id -> 80
etc...
Could you help me understand what's wrong?
federico85 30 Jan, 2011
Sorry for bothering you again but maybe I've been able of doing something better than before...
Using this code it associates cf_ids and points:
<?php

$db =& JFactory::getDBO();

$query = "
    SELECT `anno_edizione`
        FROM `#__chronoforms_scelta_edizione`;
            
";
$db->setQuery($query);
$anno_impostato = $db->loadResult();

$punti = array(100, 90, 80);


$db =& JFactory::getDBO();
$query = "
SELECT `cf_id`
FROM `#__chronoforms_iscrizioni_olimpiadi`
WHERE `check2` != '' AND `radio0` = 'M' AND `cat_cerchi` = 'D' AND `rifiuto_cerchi` = ''AND `edizione_anno` = '$anno_impostato'
ORDER BY somma_pts_cerchi DESC, date_int ASC
LIMIT 3
";
$db->setQuery($query);
$cf_ids = $db->loadResultArray();

$prova = array_combine ($cf_ids, $punti);


foreach($prova as $k=>$v) { echo "

<input type='text' name='cf_id' id='cf_id' value='$k' />  
<input type='text' name='class_gen_cerchi' id='somma_pts_cerchi' value='$v' /> <br/>"; }

?>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Conferma" name="button_0" type="submit" />
  </div>
  <div class="cfclear"> </div>
</div>


Now I don't know why when i submit the form it updates only the first record of my table. The others aren't updated except the cf_user_id (the only field i don't want to be updated)... I've been googlin' a lot but i didn't find an answer helping me to solve this problem...

Hoping that someone could help me... Thanks in advance...

Federico
GreyHead 30 Jan, 2011
Hi federico85,

I think that $result = $db->loadObjectlist(); will give you an array of objects. You can't then expect this to combine with a standard array and give you a meaningful result. Have you output $result to see exactly what you have there?

If you use $result = $db->loadResultArray(); then you should get a better result.

Bob
federico85 30 Jan, 2011
Hi Bob!
Thanks for your reply...
As you can see, in the second answer I posted, I used loadResultArray(); and using this it shows me the result i wanted to get.

Eg. cf_id -> points

Now when i try to submit the form it updates only the first record of my table. The others aren't updated except the cf_user_id (the only field i don't want to be updated)...

What can i do for updating other records avoiding the cf_user_id to be updated?

Another question:

The results i have to combine are points ( eg: 100, 90, 80, 70, 60, 50, 40, 30, 20, 10) and the first 10 position of a ranking (cf_ids).
I read that array_combine can be used only if the number of results extracted from the 2 arrays you want to combine are equal eg: 10 cf_id and 10 points. But you can't use it if you want to combine 8 cf_id and 10 points. How can i use array_combine without being sure if the number of results i could get from the cf_ids array is bigger than ten?

Thanks in advance...

Federico
This topic is locked and no more replies can be posted.