Autor Zpráva
Jack06
Profil
DObrý den, mám takový problém s výstupem z databáze:

záznam v databázi:
id  |  sex  |  age  |  music  |  charism
1  |  male  |  18  |  lidova  |  otevrenost


tabulky pak takovéto, nenapadlo mě jak jinak to udělat:
<?php 
    // table select
    $sel = new tableCache();
   print '
        <br><br>
        <table>
         <tr class="sud"> <th class="question"></th>                         <th>extroverzia</th><th>prívetivosť</th><th>svedomitosť</th><th>neuroticizmus</th><th>otvorenosť</th></tr>
<tr class="lich"><th class="question">Country & Western</th>        <th>'.$sel->extrovert('country').'</th>       <th>'.$sel->privetivost('country').'</th>       <th>'.$sel->svedomitost('country').'</th>         <th>'.$sel->neuroticizmus('country').'</th>         <th>'.$sel->otevrenost('country').'</th></tr> 
         <tr class="sud"> <th class="question">Džez</th>                     <th>'.$sel->extrovert('dzez').'</th>          <th>'.$sel->privetivost('dzez').'</th>          <th>'.$sel->svedomitost('dzez').'</th>            <th>'.$sel->neuroticizmus('dzez').'</th>            <th>'.$sel->otevrenost('dzez').'</th></tr>
         <tr class="lich"><th class="question">Latinsko-americká hudba</th>  <th>'.$sel->extrovert('latina').'</th>        <th>'.$sel->privetivost('latina').'</th>        <th>'.$sel->svedomitost('latina').'</th>          <th>'.$sel->neuroticizmus('latina').'</th>          <th>'.$sel->otevrenost('latina').'</th></tr> 
         <tr class="sud"> <th class="question">Reggae</th>                   <th>'.$sel->extrovert('reggae').'</th>        <th>'.$sel->privetivost('reggae').'</th>        <th>'.$sel->svedomitost('reggae').'</th>          <th>'.$sel->neuroticizmus('reggae').'</th>          <th>'.$sel->otevrenost('reggae').'</th></tr>
         <tr class="lich"><th class="question">Súčasná africká hudba</th>    <th>'.$sel->extrovert('africka').'</th>       <th>'.$sel->privetivost('africka').'</th>       <th>'.$sel->svedomitost('africka').'</th>         <th>'.$sel->neuroticizmus('africka').'</th>         <th>'.$sel->otevrenost('africka').'</th></tr>
         <tr class="sud"> <th class="question">R&B</th>                      <th>'.$sel->extrovert('rnb').'</th>           <th>'.$sel->privetivost('rnb').'</th>           <th>'.$sel->svedomitost('rnb').'</th>             <th>'.$sel->neuroticizmus('rnb').'</th>             <th>'.$sel->otevrenost('rnb').'</th></tr>
         <tr class="lich"><th class="question">Soul</th>                     <th>'.$sel->extrovert('soul').'</th>          <th>'.$sel->privetivost('soul').'</th>          <th>'.$sel->svedomitost('soul').'</th>            <th>'.$sel->neuroticizmus('soul').'</th>            <th>'.$sel->otevrenost('soul').'</th></tr> 
         <tr class="sud"> <th class="question">Punk</th>                     <th>'.$sel->extrovert('punk').'</th>          <th>'.$sel->privetivost('punk').'</th>          <th>'.$sel->svedomitost('punk').'</th>            <th>'.$sel->neuroticizmus('punk').'</th>            <th>'.$sel->otevrenost('punk').'</th></tr>
         <tr class="lich"><th class="question">Rock</th>                     <th>'.$sel->extrovert('rock').'</th>          <th>'.$sel->privetivost('rock').'</th>          <th>'.$sel->svedomitost('rock').'</th>            <th>'.$sel->neuroticizmus('rock').'</th>            <th>'.$sel->otevrenost('rock').'</th></tr> 
         <tr class="sud"> <th class="question">Pop</th>                      <th>'.$sel->extrovert('pop').'</th>           <th>'.$sel->privetivost('pop').'</th>           <th>'.$sel->svedomitost('pop').'</th>             <th>'.$sel->neuroticizmus('pop').'</th>             <th>'.$sel->otevrenost('pop').'</th></tr>
         <tr class="lich"><th class="question">Elektronická hudba</th>       <th>'.$sel->extrovert('elektronicka').'</th>  <th>'.$sel->privetivost('elektronicka').'</th>  <th>'.$sel->svedomitost('elektronicka').'</th>    <th>'.$sel->neuroticizmus('elektronicka').'</th>    <th>'.$sel->otevrenost('elektronicka').'</th></tr> 
         <tr class="sud"> <th class="question">Hip-Hop/ Rep</th>             <th>'.$sel->extrovert('hiphop').'</th>        <th>'.$sel->privetivost('hiphop').'</th>        <th>'.$sel->svedomitost('hiphop').'</th>          <th>'.$sel->neuroticizmus('hiphop').'</th>          <th>'.$sel->otevrenost('hiphop').'</th></tr>
         <tr class="lich"><th class="question">Zvonkohra</th>                <th>'.$sel->extrovert('zvukohra').'</th>      <th>'.$sel->privetivost('zvukohra').'</th>      <th>'.$sel->svedomitost('zvukohra').'</th>        <th>'.$sel->neuroticizmus('zvukohra').'</th>        <th>'.$sel->otevrenost('zvukohra').'</th></tr>   
        </table>
    ';  
    
?>
Jack06
Profil
a třída k výběru dat:
<?php
class tableCache{
  private $cache;

  
  public function __construct(){
  $this->db = MyDB::getDB();    $this->db->set_charset('utf8');

  $this->cache = $this->db->query("select charism, music, age, sex from statistic");
  }
  
  public function extrovert( $area ){
    $counter['e_'.$area] = 0;  $result = $this->cache;
    while($data = $result->fetch_object()){
      if($data->charism == 'extrovert' && $data->music == $area){ $counter['e_'.$area]++; }
    }
    
    return $counter['e_'.$area];
  }
  
  public function privetivost( $area ){
    $counter['p_'.$area] = 0; $result = $this->cache;
    while($data = $result->fetch_object()){
      if($data->charism == 'privetivost' && $data->music == $area){$counter['p_'.$area]++; }
    }
    
    return $counter['p_'.$area];
  }
  
  public function svedomitost( $area ){
    $counter['s_'.$area] = 0; $result = $this->cache;
    while($data = $result->fetch_object()){
      if($data->charism == 'svedomitost' && $data->music == $area){ $counter['s_'.$area]++;}
    }
    
    return $counter['s_'.$area];
  }
  
  public function neuroticizmus( $area ){
    $counter['n_'.$area] = 0; $result = $this->cache;
    while($data = $result->fetch_object()){
      if($data->charism == 'neuroticizmus' && $data->music == $area){ $counter['n_'.$area]++; }
    }
    
    return $counter['n_'.$area];
  }
  
  public function otevrenost( $area ){
    $counter['o_'.$area] = 0; $result = $this->cache;
    while($data = $result->fetch_object()){
       if($data->charism == 'otevrenost' && $data->music == $area){$counter['o_'.$area]++; } 
    } 
    
    return $counter['o_'.$area];
  }
  
}
?>


jak můžete vidět zkusil jsem ty countery i jinak označit, aby byli jedinečné ale pořád nic... V případě, že zavolám jedinou funkci na výpis hodnoty ( např $sel->otevrenost('zvukohra') ), tak to funguje dobře a ukáže počet záznamů, ale jakmile přidám více viz tabulka tak to všude ukáže nuly a nepočítá to a nevím jak to udělat díky za rady
Majkl578
Profil
Problém bude zřejmě v opětovném volání metody fetch_object. Jak se jednou projde výsledek, při dalším průchodu už vrací false, protože je na konci.
Řešení se nabízí dvě.
1) Z kódu předpokládám použití MySQLi, které nabízí metodu mysqli_result::data_seek. Tou lze resetovat pointer tak, aby ukazoval znovu na začátek.
$result->data_seek(0);

2) Cacheovat už fetchovaná data, což mi přijde ideální a jednodušší.
Jack06
Profil
Majkl578:
Jo díky, taky mě to dneska když jsem se probouzel napadlo jak to udělat :-) díky moc

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0