problema afisare

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
Avatar utilizator
evident
Mesaje:168

problema afisare

Am scriptul :

Cod: Selectaţi tot

<style type="text/css">
<!--
.style1 {font-weight: bold}
.style3 {
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
}
-->
</style>
<?php

include "connect_to_mysql.php";
$user = $_SESSION['username'];
$sql = mysql_query("SELECT id, titluanunt, textanunt, pret, moneda, categorie FROM anunturi WHERE nume='" .$user. "' ORDER BY id DESC");

$nr = mysql_num_rows($sql); 
if (isset($_GET['pn'])) { 
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']);
    
} else { 
    $pn = 1;
}

$itemsPerPage = 10;

$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) { 
    $pn = 1; 
} else if ($pn > $lastPage) { 
    $pn = $lastPage; 
}

$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '&id='.$user.'">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '&id='.$user.'">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '&id='.$user.'">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '&id='.$user.'">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '&id='.$user.'">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '&id='.$user.'">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '&id='.$user.'">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '&id='.$user.'">' . $add1 . '</a> &nbsp;';
}

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;

$sql2 = mysql_query("SELECT id, titluanunt, textanunt, pret, moneda, categorie, data_adaugari, expira FROM anunturi WHERE nume='" .$user. "' ORDER BY id DESC $limit") or die(mysql_error());

$paginationDisplay = ""; 

if ($lastPage != "1"){
    
    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';
   
    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '&id='.$cat.'"> Back</a> ';
    }
    
    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
   
    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '&id='.$cat.'"> Next</a> ';
    }
}

$outputList = '';
while($row = mysql_fetch_array($sql2)) {

    $id = $row["id"];
    $textanunt = $row["textanunt"];
    $categorie = $row["categorie"];
	$titluanunt = $row["titluanunt"];
	$data_adaugari = $row["data_adaugari"];
	$expira = $row["expira"];
	$check_pic = "anunt/$id/poza.jpg";
    $default_pic = "anunt/0/poza.jpg";
    if (file_exists($check_pic)) {
    $user_pic = "<img src=\"$check_pic\" width=\"100px\" height=\"60\" border=\"0\" />"; 
    } else {
    $user_pic = "<img src=\"$default_pic\" width=\"100px\" height=\"60\" border=\"0\" />"; 
    }
	
   $textanunt=''.$row['textanunt'].'';
   $text = ucfirst($textanunt);
	$textanuntCut = substr($text, 0, 150);
	$titluanunt=''.$row['titluanunt'].'';
   $titlu = ucfirst($titluanunt);
	$titluanuntCut = substr($titlu, 0, 87);
	$pret = $row["pret"];
	$moneda = $row["moneda"];
	



   


    $outputList .= '<table width="100%" height="1%" border="0">
  <tr>
    <td valign="top"><table width="100%" border="0" bordercolor="#CC0000" bgcolor="#CC0000">
      <tr>
        <td width="74%" bordercolor="#CC0000" bgcolor="#CC0000"><span class="style3"><a href="anunt.php?id=' . $id . '" target="_blank"><font color="white"><b>' . $titluanuntCut . '</b></font></a></span></td>
        <td width="14%" bgcolor="#CC0000"><span class="style1"><a><span class="style3"><font color="white">' . $pret . ' ' . $moneda . '</font></span></a></span></td>
      </tr>
    </table>
	  <table width="100%" border="1" cellspacing="0" bordercolor="#CC0000">
        <tr>
          <td width="20%" rowspan="2"><div style="width="100" height="60" overflow:hidden;"><a href="anunt.php?id=' . $id . '" target="_blank">' . $user_pic . '</a>&nbsp;</td>
          <td colspan="2" valign="top">' . $textanuntCut . '&nbsp;</td>
        </tr>
        <tr>
          <td width="40%" height="20" valign="top"><span class="style3">Data adaugari: ' . $data_adaugari . '&nbsp;</span></td>
          <td width="40%" valign="top"><span class="style3">Expira la: ' . $expira . '&nbsp; </span></td>
        </tr>
      </table></td>
  </tr>
</table><form action="sterge.php" method="post" enctype="multipart/form-data">
<select name="sterge">
 <option value="' . $id . '">Sterge</option>
 </select>
<input type="submit" name="Sterge" value="sterge"/> </form>
';  



$outputList .= '';
   
}
Care imi afiseaza anunturile userului logat.
Problema e ca daca nu are nici un anut userul care e logat si intra la pagina cu anunturile lui imi da eroare in linia 81

Cod: Selectaţi tot

while($row = mysql_fetch_array($sql2)) {
cand are anunturi nu da nici o eroare...dar cand nu are da eroare
Daca pun codul ala sa imi afiseze eroare imi zice asa:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-10,10' at line 1
Multumesc

MarPlo Mesaje:4343
Salut,
Instructiunea while() care parcurge rezultatul de la mysql ar trebui executata doar daca exista cel putin un rand in raspunsul de la mysql.
Adica asa:

Cod: Selectaţi tot

// daca e cel putin un rand
if (mysql_num_rows($sql2) > 0) {
  while($row = mysql_fetch_array($sql2)) {
    // preia datele din $row
  }
}
else echo '0 randuri';

evident Mesaje:168
am pus asa si nu vrea

Cod: Selectaţi tot

if (mysql_num_rows($sql2) > 0) {
while($row = mysql_fetch_array($sql2)) {
    $id = $row["id"];
    $textanunt = $row["textanunt"];
    $categorie = $row["categorie"];
    $titluanunt = $row["titluanunt"];
    $data_adaugari = $row["data_adaugari"];
    $expira = $row["expira"];
    $check_pic = "anunt/$id/poza.jpg";
    $default_pic = "anunt/0/poza.jpg";
    if (file_exists($check_pic)) {
    $user_pic = "<img src=\"$check_pic\" width=\"100px\" height=\"60\" border=\"0\" />"; 
    } else {
    $user_pic = "<img src=\"$default_pic\" width=\"100px\" height=\"60\" border=\"0\" />"; 
    }
    
   $textanunt=''.$row['textanunt'].'';
   $text = ucfirst($textanunt);
    $textanuntCut = substr($text, 0, 150);
    $titluanunt=''.$row['titluanunt'].'';
   $titlu = ucfirst($titluanunt);
    $titluanuntCut = substr($titlu, 0, 87);
    $pret = $row["pret"];
    $moneda = $row["moneda"];

    $outputList .= '<table width="100%" height="1%" border="0">
  <tr>
    <td valign="top"><table width="100%" border="0" bordercolor="#CC0000" bgcolor="#CC0000">
      <tr>
        <td width="74%" bordercolor="#CC0000" bgcolor="#CC0000"><span class="style3"><a href="anunt.php?id=' . $id . '" target="_blank"><font color="white"><b>' . $titluanuntCut . '</b></font></a></span></td>
        <td width="14%" bgcolor="#CC0000"><span class="style1"><a><span class="style3"><font color="white">' . $pret . ' ' . $moneda . '</font></span></a></span></td>
      </tr>
    </table>
      <table width="100%" border="1" cellspacing="0" bordercolor="#CC0000">
        <tr>
          <td width="20%" rowspan="2"><div style="width="100" height="60" overflow:hidden;"><a href="anunt.php?id=' . $id . '" target="_blank">' . $user_pic . '</a>&nbsp;</td>
          <td colspan="2" valign="top">' . $textanuntCut . '&nbsp;</td>
        </tr>
        <tr>
          <td width="40%" height="20" valign="top"><span class="style3">Data adaugari: ' . $data_adaugari . '&nbsp;</span></td>
          <td width="40%" valign="top"><span class="style3">Expira la: ' . $expira . '&nbsp; </span></td>
        </tr>
      </table></td>
  </tr>
</table><form action="sterge.php" method="post" enctype="multipart/form-data">
<select name="sterge">
 <option value="' . $id . '">Sterge</option>
 </select>
<input type="submit" name="Sterge" value="sterge"/> </form>';

$outputList .= '';   
}
}
else echo 'Nu ai nici un anut'; 
eroarea este aici:

Cod: Selectaţi tot

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
stie cineva ce as putea sa fac?
Multumesc

MarPlo Mesaje:4343
Testeaza cu un "echo" la variabila ce contine comanda SQL trimisa la MySQL, ca sa vezi exact ce trimite.

evident Mesaje:168
daca nu am nici un anunt $pn ia valoarea 0 iar la calcul acolo o sa fie (0-1)*10 deci va trimite -10,10 si da eroare...-10,10 trimite , am testat

MarPlo Mesaje:4343
In cazul asta, defineste o valoare care vrei pt. $pn cand e 0.

Cod: Selectaţi tot

if($pn == 0) $pn = 1;        // sau 2
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

evident Mesaje:168
asa am facut si eu dar imi afiseaza jos la paginare asa:
-1 0 Next
cum sa fac sa nu imi mai apara paginarea daca nu are nici un rezultat?

MarPlo Mesaje:4343
Incearca intai un select care doar sa returneze numarul de randuri, apoi, daca acel numar e mai mare ca 0, faci select-ul si operatiile de paginare.
Ceva asa:

Cod: Selectaţi tot

// Afla cate linii sunt in tabel (MODIFICATI 'nume_tb'), eventual si cu o anumita conditie WHERE
$sql = "SELECT COUNT(*) FROM `nume_tb` WHERE conditie";  
$result = mysql_query($sql, $conn) or trigger_error(E_USER_ERROR);  
$r = mysql_fetch_row($result);  
$numrows = $r[0];

// daca sunt randuri
if($numrows > 0) {
  // Select si efectuare paginare
} 
Sau vezi si script-ul: Script paginare.

Subiecte similare