I’d suspect your array is NOT returning the data you think, or you’ve already indexed it somehow using a command like ‘end’.
Test 1: right before you do your WHILE statement:
echo ‘<pre>’,print_r($array),‘</pre>’;
to make sure the values you want to output are actually in there. If they are… are you using any of the various array iteration commands before it? I’m not seeing them in the code, but that would be my only other guess.
You might also want to get the final > on your </table> – you missed it. (easy typo fix)… and if you use comma delimits on your echo instead of string addition it will behave more predictably and execute faster. Only use string addition (periods) when you HAVE TO, not ‘just because’ as it consumes much more memory (since the string has to be built completely before being sent to echo) meaning more time wasted on garbage collection.
… and lands sake man, get some sanitization in there (you’ve got mysqli, prepare those queries instead of string building!)
You know, looking at this, you’re building an array for no reason… waste of memory when you can use “seek” to go back to record zero.
This is untested, probably has a few typo’s, but here’s how I’d be doing that:
<?php
if ($conn=mysqli_connect('localhost', 'root', 'root', 'table')) {
if (
(isset($_GET['name']) && isset($_GET['surname']) ||
isset($_GET['province'])
) {
if (isset($_GET['province'])) {
$queryString='
SELECT type, name, surname, cf, city, province
FROM users
WHERE province=?
';
if ($_GET['member']==1) {
$queryString.=' AND member=1';
} else if ($_GET['member']!=2) {
$queryString.=' AND member=0';
}
$statement=$conn->prepare($queryString);
$statement->bindParam('s',$_GET['province']);
} else {
$statement=$conn->prepare('
SELECT type, name, surname, cf, city, province
FROM users
WHERE name=? AND surname=?
');
$statement->bind_param('ss',$_GET['name'],$_GET['surname']);
}
$result=$statement->execute();
if (($result->num_rows)==0) {
echo '
<p>
Nessun utente trovato.
<a href="search.php">Effettua una nuova ricerca</a>
</p>';
} else {
echo '
<a href="#" id="print" onclick="window.print()">
Stampa lettere ed etichette per questi utenti
</a>
<table>';
while ($row=$result->fetch_assoc()) {
echo '
<tr>
<td><a href="">Modifica</a></td>
<td><a href="',sprintf(
'user.php?%s',
http_build_query(array('cf' => $row['cf'])
),'">Visualizza</a></td>
<td>',$row["name"],'</td>
<td>',$row["surname"],'</td>
<td>',$row["cf"],'</td>
<td>',$row["city"],'</td>
<td>',$row["province"],'</td>
</tr>';
}
echo '
</table>';
$result->data_seek(0); // reset results so we can fetch a second time
while ($row=result->fetch_assoc()) {
echo '
<div id="letter">
<ul id="member_address">
<li>',$row["name"],' ',$row["surname"],'</li>
<li>Via ',$row["street"],' ',$row["number"],'</li>
<li>',$row["cap"],' ',$row["city"],' (',$row["province"],')</li>
</ul>
<ul id="onlus_address">
<li>Nome onlus</li>
<li>Indirizzo onlus</li>
</ul>
<p class="letterhead">
Alla cortese attenzione del
',$row["type"],'
',$row["name"],'
',$row["surname"],',
</p><p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum odio metus, luctus non scelerisque quis, sagittis ac nibh. Aliquam euismod, odio a ultricies consequat, lacus urna laoreet nisl, sit amet rhoncus dui ante at dolor. Nullam justo lectus, rhoncus ut porttitor quis, pellentesque nec quam. Curabitur velit neque, molestie eu bibendum eget, eleifend sed ligula. Nam pulvinar ornare dolor et sodales. Pellentesque semper risus vitae justo ultricies in varius magna commodo. Curabitur accumsan faucibus est, ac suscipit massa tempus ac. Duis cursus eleifend velit, a sagittis velit facilisis ullamcorper. In placerat ultricies dui vitae rhoncus. Maecenas consectetur mi ac ligula viverra vitae adipiscing dui consequat. Mauris vel erat ipsum, quis consectetur nunc. Etiam facilisis hendrerit nisl, sed lacinia leo facilisis ut. In quis mauris arcu, ac fringilla sapien. Maecenas eget nibh risus. Nulla hendrerit, orci ut posuere fermentum, tortor lacus porttitor nisl, nec hendrerit metus neque eu augue.
</p><p>
Nunc varius porta libero, ac porta purus tempus et. Aenean gravida dignissim posuere. Maecenas dapibus diam vitae felis molestie in interdum eros mattis. Cras id leo ligula, eget feugiat ligula. Suspendisse posuere massa quis nisl imperdiet ac viverra leo ultrices. Morbi sapien quam, ultricies bibendum varius vel, dictum in urna. Proin magna leo, sollicitudin dignissim aliquam eu, suscipit vel magna. Nam justo purus, gravida non pharetra faucibus, feugiat id mauris. Quisque condimentum, mauris at convallis ornare, mi mauris mollis lacus, vitae hendrerit odio eros nec nibh. Mauris a tempus est. Curabitur est elit, vulputate et dignissim nec, vulputate ornare ipsum. Fusce ligula libero, pulvinar vel sollicitudin et, faucibus quis tellus. Donec at ligula libero, ac feugiat dui. Nulla quis orci eget mi consequat commodo in malesuada orci. Etiam eget leo iaculis nisi pretium ultrices. Ut laoreet sapien sed leo luctus porttitor.
</p><p>
Cordiali saluti,
</p><p id="place_date">
Luogo, lì ' . date('d/m/Y') . '
<p><p id="signature">
Firma
</p>
</div>';
}
}
} else { // no $_GET passed, show form
echo '
<h2>Ricerca</h2>
<h3>Per nome e cognome</h3>
<form action="" method="get">
<label for="name">Nome</label>
<input type="text" name="name" />
<label for="surname">Cognome</label>
<input type="text" name="surname" />
<input type="submit" value="Cerca" />
</form>
<h3>Per provincia</h3>
<form action="" method="get">
<label for="province">Provincia</label>
<input type="text" name="province" />
<input type="radio" name="member" value="2" checked="checked" />Tutti
<input type="radio" name="member" value="1" />Solo iscritti
<input type="radio" name="member" value="0" />Non iscritti
<input type="submit" value="Cerca" />
</form>';
}
} else die(''Database Error: '.mysqli_connect_error());
?>
Played with the logic order a bit to simplify it down and make it clearer… and by using prepared queries from the start all your pesky untrustworthy user input is nice and sanitized for you.
The ‘big’ thing though being this:
$result->data_seek(0); // reset results so we can fetch a second time
which lets you:
while ($row=result->fetch_assoc()) {
A second time! That way you don’t need an extra array. One of the few things mysqli can do that PDO can’t.