PHP paging with problems

Asked

Viewed 38 times

0

I have the following problem: I am starting to maintain the site of a certain research from my university. I am a beginner in PHP. The problem is that for example: When in the filter you select "Sick", you have to see all sick patients, it on the first page does this, and leaves the "Sick" option in the selected filter, in addition to putting page "1" selected and page "2" as a link to the other records. When you click on "2" it exits the filter and selects all the records, "Sick", "Healthy", etc... How to do? Below all the code of the page index.php

<?php
    include("include/sesion.php");
    $total_paginas=0;
    $registros=$limite=30;
    $inicioM = 0; 
    $paginaM = 1;   
    $page=1;
    // pagina pedida 
    if (isset($_GET['pagina'])){ 
        $page = (int) $_GET["pagina"];  
        if ($page < 1){  
            $page = 1;  
        }  
    }else{ 
        $page = 1; 
    }
    $inicioM = ($page - 1) * $registros;
    $paginaM = $page;

    $MTtitulo="Prontuarios";
    $MTdescripcion="Prontuarios médicos.";
    include("include/top.php");
?>
<div class="span2">
<?php
    include("include/menu.php");
?>   
</div>
<div class="span10 bloco">
<?php
    $url="index.php?";
    $oprontuario=new CObjeto();
    $oprontuario->addLog($iduser, "Listado de pacientes");

    $prontuarios=array();
    $filtro=0;

    if(isset($_GET['filtro'])){
        $filtro=$_GET['filtro'];
        $url="index.php?filtro=".$filtro;
    }

    $order=1;
    $ordenarPor="id";
    if(isset($_GET['order'])){
        $order=$_GET['order'];
        switch ($order) {
            case 1: $ordenarPor="id";
            break;
            case 2: $ordenarPor="date_birth";
            break;
            case 3: $ordenarPor="name";
            break;
        }
        $url.="&order=".$order;
    }
    $by=1;
    if(isset($_GET['by'])){
        $by=$_GET['by'];
        if($by==2){
            $ordenarPor.=" desc";
            $url.="&by=".$by;
        }
    }
    $diag="";
    $diagnosis="";
    if(isset($_GET['diag'])){
        if($_GET['diag'] == 'X'){
            $escolhido = "X";
        }else {
            $diag=$_GET['diag'];
        }
        if(strlen($diag)>0){
            $diagnosis=" and visit.diagnosis='".$diag."'";
        }
    }
    $prontuarios=$oprontuario->listarProntuario($filtro, $diagnosis);
    $total=$oprontuario->getTotalProntuarios();
    $total_paginas = ceil($total / $registros); 
    $offset = ($page-1) * $limite; 
    $prontuarios=$oprontuario->listarProntuarioPag($offset,$limite,$filtro, $ordenarPor, $diagnosis);
    $i=0;
    ?>
    <div class="row-fluid">
        <div class="span5 form-inline"><strong>Show:</strong>
            <select id="filtro" class="span9">
                <option value="0" <?php if($filtro==0) echo "selected"?>>All patients</option> 
                <option value="1" <?php if($filtro==1) echo "selected"?>>Patients with mammography</option> 
                <!--  <option value="2" <php if($filtro==2) echo "selected"?>>Patients with ultrasound</option> 
                <option value="3" <php if($filtro==3) echo "selected"?>>Patients with magnetic resonancessonância magnética</option> 
                -->
                <option value="4" <?php if($filtro==4) echo "selected"?>>Patients without examination</option> 
            </select>
        </div>
        <div class="span4 form-inline"><strong>Sort by:</strong>
            <select id="order" class="span6">
                <option value="1" <?php if($order==1) echo "selected"?>>Registration date</option> 
                <option value="2" <?php if($order==2) echo "selected"?>>Age</option> 
                <option value="3" <?php if($order==3) echo "selected"?>>Name</option> 
            </select>
            <?php if($by==2){
            if (strpos($url,"&by=2")>=0) $url=str_replace("&by=2","",$url);
            ?>
            <a href="<?php echo $url."&by=1"?>" title="Sort ascending" class="flechas"><i class="icon-arrow-up"></i></a>
            <?php }else{
            if (strpos($url,"&by=1")>=0) $url=str_replace("&by=1","",$url);
            ?>
            <a href="<?php echo $url."&by=2"?>" title="Sort downwards" class="flechas"><i class="icon-arrow-down"></i></a>
            <?php } ?>
        </div>

        <div class="span3 pull-right form-inline">
            <?php
            if($_SESSION['id_user']==0){
            ?>
                <p align="right"><a href="addpr.php" class="btn btn-danger"><i class="icon-user icon-white"></i> Adicionar Paciente</a></p>    
            <?php
            }
            ?>
        </div>
        <div class="span5 form-inline"><strong>Diagnostic:</strong>
            <select id="diagnosis" class="span8">
                <option value="" <?php if($diag=="") echo "selected"?>>All</option>
                <option value="S" <?php if($diag=="S") echo "selected"?>>Healthy</option>
                <option value="D" <?php if($diag=="D") echo "selected"?>>Sick</option>
                <option value="U" <?php if($diag=="U") echo "selected"?>>Unknown</option>
                <!--<option value="X" <?php if($diag=="") echo "selected"?>>Teste</option> -->
            </select>
        </div>
    </div>  

    <table width="100%" id="mytable" class="bordered" >
        <tr>
            <th>ID</th>
            <th>Records</th>
            <th>Name</th>        
            <th>Age</th>
            <th>Exams</th>
            <th>Diagnosis</th>
            <th>Action</th>
        </tr>
        <?php
        foreach($prontuarios as $prontuario)
        {
            if($i % 2 == 0)
                $class="spec";
            else
                $class="alt";
                $i++;
                $exames="";
            if($prontuario['termografia']>0)
                $exames.="Thermography";
            if($prontuario['mamografia']>0)
                $exames.=", mammography";

            $dianostico="Unknown";
            if($prontuario['diagnosis']=="S")
                $dianostico="Healthy";
            if($prontuario['diagnosis']=="D")
                $dianostico="Sick";
            ?>
            <tr>
                <td class="<?php echo $class?>"><?php echo $prontuario['id']?></td>
                <td class="<?php echo $class?>"><?php if(strlen($prontuario['record']>0)) echo $prontuario['record']; else echo "Voluntária";?></td>
            <?php if($_SESSION['id_user']==0){?>  
                <td class="<?php echo $class?>"><a href="details.php?id=<?php echo $prontuario['id']?>" title="Show record"><?php echo utf8_encode($prontuario['name']); ?></a></td>
            <?php } else{  ?>
                <td class="<?php echo $class?>"><a href="details.php?id=<?php echo $prontuario['id']?>" title="Show record">PRIVATE</a></td>
            <?php } ?>
                <td class="<?php echo $class?>"><?php echo calcularedad(($prontuario['date_birth']))?></td>
                <td class="<?php echo $class?>"><?php echo $exames?></td>
                <td class="<?php echo $class?>"><?php echo $dianostico?></td>
                <td class="<?php echo $class?>"><a href="details.php?id=<?php echo $prontuario['id']?>" title="Show record"><i class="flaticon-zoom19"></i></a>&nbsp;
            <?php if($_SESSION['id_user']==0){ ?>
                <a href="modify.php?id=<?php echo $prontuario['id']?>" title="Editar Prontuario"><i class="flaticon-writing9"></i></a>&nbsp; 
                <a href="javascript:delpr(<?php echo $prontuario['id'] ?>)" title="Apagar Prontuario"><i class="flaticon-trash30"></i></a>&nbsp; 
            <?php
            }
            ?>
            </td>
        <?php
        }
        ?>
    </table>
    <div class="pagination pagination-centered">
        <ul>
            <?php
            $url="index.php?filtro=".$filtro;
            $url.="&order=".$order;
            $url.="&by=".$by;
            if($total_paginas>1) {
                if(($paginaM - 1) > 0) {
                    echo "<li><a href='".$url."&pagina=".($paginaM-1)."'>Previous</a></li> ";
                }
                for ($i=1; $i<=$total_paginas; $i++){ 
                    if ($paginaM == $i){
                        echo "<li><span class='activo'>".$paginaM."</span></li> ";
                    }
                    else {
                        echo "<li><a href='".$url."&pagina=".$i."'>$i</a></li> ";
                    }
                }
                if(($paginaM + 1)<=$total_paginas) {
                    echo "<li><a href='".$url."&pagina=".($paginaM+1)."'>Next</a><li>";
                }
            }
            ?> 
        </ul>
    </div><!--fin paginado-->
</div>
<script>
    $('.bordered tr').mouseover(function(){
        $(this).addClass('highlight');
    }).mouseout(function(){
        $(this).removeClass('highlight');
    });
    function delpr(id)
    {
        var answer = confirm ("Tem certeza que deseja apagar o prontuario? \nTambém serão apagados as visitas do paciente");
        if (answer)
        window.location="delete.php?id="+id
    }

    $(document).ready(function() {
        $("#filtro").change(function(){
            window.location="index.php?filtro="+this.value+"&order=<?php echo $order?>";
        });
        $("#order").change(function(){
            window.location="index.php?filtro=<?php echo $filtro?>&order="+this.value;
        });
        $("#diagnosis").change(function(){
            window.location="index.php?filtro=<?php echo $filtro?>&diag="+this.value;
        });
    });
</script>
<?php
    include("include/end.php");
?>
  • you need to pass the parameters to page 2, I am analyzing the code here to help you

  • send as the page 2 link in the browser

  • <a href='$url&pagina=".($paginaM+1)."'>, you just pass the page, but do not pass the current filter.

  • @Andersoncarloswoss this one $url There at the beginning is the $_GET that picks up the filter, or not?

  • @Weessmith But it seems that the filter in question is defined by $_GET['diag'], that is not added to the URL.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.