PHP - cambiar codigo codeigniter

 
Vista:

cambiar codigo codeigniter

Publicado por lorena (8 intervenciones) el 23/02/2015 17:43:59
Buenas tardes a todos;

tengo un histórico de noticias creado pero lo estoy intentando pasar a codeigniter y no hay forma, me podriais ayudar que me estoy volviendo loca. Gracias

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php require_once('conexiones/conexionblog.php'); ?>
<?php
$query_DatosMuro = "SELECT fchFecha, idNoticia, strTitulo FROM `tblnoticias` ORDER BY fchFecha DESC";
$DatosMuro = mysqli_query($conexionblog, $query_DatosMuro) or die(mysqli_error());
$totalRows_DatosMuro = mysqli_num_rows($DatosMuro);
$nav = array();
while ( ($fila = mysqli_fetch_array($DatosMuro)) > 0 ){
foreach($fila as $news){
$year = date(fechaporyear($fila['fchFecha']));
$month = fechapormes($fila['fchFecha']);
$nav[$year][$month][$fila['idNoticia']] = $fila['strTitulo'];
}
}
 
///
<section class="hemeroteca">
      <hgroup><h1 class="title">Hemeroteca</h1></hgroup>
  <ul>
    <?php
    foreach ($nav as $k => $v) {
    ?>
        <li class="primero"><?php echo $k ?>
            <ul>
            <?php
            foreach ($v as $k2 => $v2) {
                ?>
                <li><?php echo $k2.' ('.sizeof($v2).')' ?>
                    <ul>
                        <?php
                        foreach ($v2 as $k3 => $v3) {
                        ?>
                            <li><a href="noticias_anteriores.php?ID=<?php echo $k3 ?>"><?php echo $v3 ?></a></li>
                        <?php
                        }
                        ?>
                    </ul>
                </li>
                <?php
            }
            ?>
            </ul>
        </li>
        <?php
    }
    ?>
</ul>

Codeigniter modelo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php //
class Hemeroteca_model extends CI_Model {
    public function __construct() {
        parent::__construct();
 
    }
    function lista_noticias($idNoticia) {
        $this->db->select('idNoticia',$idNoticia,'fchFecha','strTitulo','url_Noticia');
        $this->db->group_by(Month('fchFecha'), Year('fchFecha'));
          $consulta = $this->db->get('tblnoticias');
         while($q->result() > 1)
{
 
         foreach($consulta as $row)
 
        $year = date(fechaporyear($row['fchFecha']));
        $month = fechapormes($row['fchFecha']);
        $nav[$year][$month][$row['idNoticia']] = $row['strTitulo']; // add each user id to the array
    }
 
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

cambiar codigo codeigniter

Publicado por Manuel Emilio Vargas Herrera (154 intervenciones) el 23/02/2015 18:35:12
No conozco de codeigniter, pero me parece que podrías probar el siguiente par de cambios:

1
2
3
4
5
6
7
while ( ($fila = mysqli_fetch_array($DatosMuro)) > 0 ){
foreach($fila as $news){
$year = date(fechaporyear($fila['fchFecha']));
$month = fechapormes($fila['fchFecha']);
$nav[$year][$month][$fila['idNoticia']] = $fila['strTitulo'];
}
}

por...

1
2
3
4
5
while ( ($fila = mysqli_fetch_array($DatosMuro)) > 0 ){
$year = date(fechaporyear($fila['fchFecha']));
$month = fechapormes($fila['fchFecha']);
$nav[$year][$month][$fila['idNoticia']] = $fila['strTitulo'];
}

y...

1
2
3
4
5
6
7
8
9
10
11
12
while($q->result() > 1)
{
 
foreach($consulta as $row)
 
$year = date(fechaporyear($row['fchFecha']));
$month = fechapormes($row['fchFecha']);
$nav[$year][$month][$row['idNoticia']] = $row['strTitulo']; // add each user id to the array
}
 
 
}

por ...

1
2
3
4
5
6
foreach($consulta as $row)
{
$year = date(fechaporyear($row['fchFecha']));
$month = fechapormes($row['fchFecha']);
$nav[$year][$month][$row['idNoticia']] = $row['strTitulo']; // add each user id to the array
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

cambiar codigo codeigniter

Publicado por MMan (59 intervenciones) el 23/02/2015 19:45:37
hola...

1
2
3
4
5
6
7
while ( ($fila = mysqli_fetch_array($DatosMuro)) > 0 ){
foreach($fila as $news){
$year = date(fechaporyear($fila['fchFecha']));
$month = fechapormes($fila['fchFecha']);
$nav[$year][$month][$fila['idNoticia']] = $fila['strTitulo'];
}
}

me pacere que el foreach esta demas... la variable $news ni lo usas dentro de ese bloque

aqui me perdi
1
2
3
4
5
6
7
8
9
10
11
foreach ($nav as $k => $v) {
?>
<li class="primero"><?php echo $k ?>
<ul>
<?php
foreach ($v as $k2 => $v2) {
?>
<li><?php echo $k2.' ('.sizeof($v2).')' ?>
<ul>
<?php
foreach ($v2 as $k3 => $v3) {


no se si lo puedes explicar, si es de manera grafica y con ejemplos mucho mejor, SUPONGO que uno te devuelve el año, el otro el mes.. el ultimo la noticia
---------------

function lista_noticias($idNoticia) {


supongo que esa funcion filtra por $idNoticia o devuelve todas las noticias ?
por que en el primer ejemplo (sin CI ) no tienes ningun filtro
-----------

en tu select de arriba, veo que NO TIENES ninguna AGRUPACION (GROUP BY), pero si lo estas haciendo en el CI (code igniter)
--------------

me parece que deberia ser asi...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function lista_noticias($idNoticia) {
='editor_indent'>$this->db->select('fchFecha, idNoticia, strTitulo');
$this->db->order_by("fchFecha", "desc");
 
// idNoticia es filtro ??
// si hay algun parametro filtra el select
if ($idNoticia <> "") {
   $this->db->where('idNoticia', $idNoticia);
}
 
$query = $this->db->get('tblnoticias');
 
if ($query->num_rows() > 0)
{
   foreach ($query->result() as $row)
   {
       // echo $row->title;
	$year = date(fechaporyear($row->fchFecha));
	$month = fechapormes($row->fchFecha);
   }
 
//// o en formato array
//   foreach ($query->result_array() as $row)
//   {
//	$year = date(fechaporyear($row['fchFecha']));
//	$month = fechapormes($row['fchFecha']);
//   }
 
}
}
 
// referencias
//https://ellislab.com/codeIgniter/user-guide/database/active_record.html
//https://ellislab.com/codeIgniter/user-guide/database/results.html

aunque tal ves este errado,,, xq de CI no se nada
salu2
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

cambiar codigo codeigniter

Publicado por lorena (8 intervenciones) el 26/02/2015 13:08:55
Gracias a todos,

La consulta en el modelo ya la consegui hacer correctamente

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php //
class Hemeroteca_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->helper('ayuda_helper');
    }
    function lista_noticias() {
 
        $query = $this->db->query("SELECT fchFecha,idNoticia, strTitulo, url_Noticia FROM `tblnoticias` ORDER BY fchFecha DESC");
        //return $query->result_array();
        {
        $array = $query->result_array();
        foreach ($query->result_array() AS $row){
        $year = fechaporyear($row['fchFecha']);
        $month = fechapormes($row['fchFecha']);
        $array[$year][$month][$row['url_Noticia']] = $row['strTitulo'];
 
        }
        return $query->result();
        }
    }
  }

Pero ahora en la vista me duplica todo,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<section class="hemeroteca">
      <hgroup><h1 class="title">Hemeroteca</h1></hgroup>
          <ul>
              <?php
            foreach ($noticia as $item):
                ?>
 
              <li class="primero"><?php echo fechaporyear($item->fchFecha);?></li>
 
           <ul>
 
               <?php
            foreach ($noticia as $item):
                ?>
 
                <li><?php echo fechapormes($item->fchFecha).' ('.sizeof($noticia).')'; ?>
           <ul>
                <?php
            foreach ($noticia as $item):
                ?>
 
            <li><?php echo $item->strTitulo; ?></li>
              <?php endforeach; ?>
                            </ul>
 
                    </li>
               <?php endforeach; ?>
            </ul>
          </li>
 
        <?php endforeach; ?>
 </ul>
</section>

Gracias a todos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar