Home > Back-end >  I have an error in my code that does not allow me to show the results and/or show an error message i
I have an error in my code that does not allow me to show the results and/or show an error message i

Time:07-03

I have a little problem that might be syntax (since I'm not that good at php). Basically, I'm de-wrapping a code where there will be records where there will be a date (in this case, a foundation date, for example; 20-08-2027). So I created an "if". If there are records with the date, for example, 2027, the records appear. If there is not, then an error message will be displayed saying that there is still no record made with that date. I've tried a few things, but nothing worked. At this point, an error appears. Below will be the error. Please try to help me. It's no use saying more technical things, because I'm not that good at php. Thank you.

Error: "Parse error: syntax error, unexpected 'else' (T_ELSE) in /home/host/public_html/template/year/2027.php on line 300"

2027.php

  <section >
        <div >
            <h1 >Eventos</h1>
            <div >
                
                <div >
                    
                    <div  id="products">
                        
                       <div >
                
                <div  id="products">
                    
                    <?php
                
                $sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
                $resu=mysqli_query($con,$sqli);
                mysqli_set_charset($con, 'UTF8');
                    
                if (mysqli_num_rows($resu)>0) {
                    while($regi=mysqli_fetch_array($resu)){
                    $sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
                    $resu_consulta=mysqli_query($con,$sqli_consulta);
                   
                    $regi_consulta=mysqli_fetch_array($resu_consulta);
                    $linkk='../eventoindividual.php?id='.$regi_consulta['id'];
                    ?>
                
                    <div >
                        <article >
                            <div >
                                <div >
                                    <div >
                                        <div ></div>
                                        <a href="<?php echo $linkk; ?>" ></a>
                                        <img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
                                        
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        <h3><?php echo $regi['nome']; ?></h3>
                                        <br>
                                        <span >
                                            <ins><span ><?php echo $regi['preco']; ?></span></ins>
                                        </span>
                                        
                                        <div >
                                            <a href="<?php echo $regi['linkbilheteira']; ?>" ><i ></i>Comprar bilhetes</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
                    </div>
    
                    <?php
                 } else{
                        ?>
                    <p>Nada!</p>
                    <?php
                    }
                }
            ?>
                    
                </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>    
        
    </section>

CodePudding user response:

The reason you are getting the error because you have while loop in your if block that you are not closing.

Check the code below for this fixed version.

<?php           
    $sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
    $resu=mysqli_query($con,$sqli);
    mysqli_set_charset($con, 'UTF8');
        
    if (mysqli_num_rows($resu)>0):
        while($regi=mysqli_fetch_array($resu)):
        $sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
        $resu_consulta=mysqli_query($con,$sqli_consulta);
        
        $regi_consulta=mysqli_fetch_array($resu_consulta);
        $linkk='../eventoindividual.php?id='.$regi_consulta['id'];
        ?>
    
        <div >
            <article >
                <div >
                    <div >
                        <div >
                            <div ></div>
                            <a href="<?php echo $linkk; ?>" ></a>
                            <img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
                            
                        </div>
                    </div>
                    <div >
                        <div >
                            <h3><?php echo $regi['nome']; ?></h3>
                            <br>
                            <span >
                                <ins><span ><?php echo $regi['preco']; ?></span></ins>
                            </span>
                            
                            <div >
                                <a href="<?php echo $regi['linkbilheteira']; ?>" ><i ></i>Comprar bilhetes</a>
                            </div>
                        </div>
                    </div>
                </div>
            </article>
        </div>

        <?php endwhile; else: ?>
        <p>Nada!</p>
<?php 
    endif;
?> 

CodePudding user response:

Move the last } bracket above }else{

 <section >
        <div >
            <h1 >Eventos</h1>
            <div >
                
                <div >
                    
                    <div  id="products">
                        
                       <div >
                
                <div  id="products">
                    
                    <?php
                
                $sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
                $resu=mysqli_query($con,$sqli);
                mysqli_set_charset($con, 'UTF8');
                    
                if (mysqli_num_rows($resu)>0) {
                    while($regi=mysqli_fetch_array($resu)){
                    $sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
                    $resu_consulta=mysqli_query($con,$sqli_consulta);
                   
                    $regi_consulta=mysqli_fetch_array($resu_consulta);
                    $linkk='../eventoindividual.php?id='.$regi_consulta['id'];
                    ?>
                
                    <div >
                        <article >
                            <div >
                                <div >
                                    <div >
                                        <div ></div>
                                        <a href="<?php echo $linkk; ?>" ></a>
                                        <img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
                                        
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        <h3><?php echo $regi['nome']; ?></h3>
                                        <br>
                                        <span >
                                            <ins><span ><?php echo $regi['preco']; ?></span></ins>
                                        </span>
                                        
                                        <div >
                                            <a href="<?php echo $regi['linkbilheteira']; ?>" ><i ></i>Comprar bilhetes</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
                    </div>
    
                    <?php
                    } // while()
                 } else{
                        ?>
                    <p>Nada!</p>
                    <?php
                    }
//                } //wrong bracket
            ?>
                    
                </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>    
        
    </section>

For your own sanity it is a good idea leave a comment after closing bracket of a long code, so you know which block it belongs to.

CodePudding user response:

Compiler can't be wrong, if it says there's syntax error then there is. The bracket before the else is closing the while loop - hence else cannot be used there, close the if condition. Here's the corrected code:

 <section >
        <div >
            <h1 >Eventos</h1>
            <div >
                
                <div >
                    
                    <div  id="products">
                        
                       <div >
                
                <div  id="products">
                    
            <?php
                
                $sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
                $resu=mysqli_query($con,$sqli);
                mysqli_set_charset($con, 'UTF8');
                    
                if (mysqli_num_rows($resu)>0) {
                    while($regi=mysqli_fetch_array($resu))  {
                    $sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
                    $resu_consulta=mysqli_query($con,$sqli_consulta);
                   
                    $regi_consulta=mysqli_fetch_array($resu_consulta);
                    $linkk='../eventoindividual.php?id='.$regi_consulta['id'];
            /* 2 brackets { */        
            ?> 
                
                    <div >
                        <article >
                            <div >
                                <div >
                                    <div >
                                        <div ></div>
                                        <a href="<?php echo $linkk; ?>" ></a>
                                        <img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
                                        
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        <h3><?php echo $regi['nome']; ?></h3>
                                        <br>
                                        <span >
                                            <ins><span ><?php echo $regi['preco']; ?></span></ins>
                                        </span>
                                        
                                        <div >
                                            <a href="<?php echo $regi['linkbilheteira']; ?>" ><i ></i>Comprar bilhetes</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
                    </div>
    
                    <?php
                 }  //  while loop
                }   //  if condition
                else {
                        ?>
                    <p>Nada!</p>
                    <?php
                }
                
            ?>
                    
                </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>    
        
    </section>

hope this helps.

  • Related