Displaying PHP results? - php

I need a little help here with some php.
just a little explanation:
(im trying to display 5 results using this code
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
} else {
header("Location: vendi.php");
}
echo '<br />Back to Archive';
}
It does display but i want that 1st result to be (image+title of the news and other results to be just title).
Hope i wrote it clearly what i needed help with.
Thank you

Your SQL statement is only fetching a single row. This isn't a complete solution, but should get you closer:
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$n_id = (int)$_GET['id'];
$path = '';
$count = 0;
$sql = 'SELECT * FROM test2 WHERE id BETWEEN ' . $n_id ' AND ' . ($n_id + 5);
$news = mysql_query($sql);
while ($result = mysql_fetch_array($news)) {
$title = $result['title'];
$date = $result['date'];
echo '<b>' . $title . ' | ' . $date . '</b>';
if ($count < 1) {
echo '<br/><img src="images.php?id='. $n_id .'>';
$count++;
}
}
if ($count == 0) { header("Location: vendi.php"); }
echo '<br />Back to Archive';
}

This should do it.
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
$first = TRUE;
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
if($first == TRUE) {
echo '<b>' . $title . ' | ' . $date . '</b>';
$first = FALSE;
}
else {
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
}
}
else {
header("Location: vendi.php");
}
echo '<br />Back to Archive';
}
}

While looping through your results, you can check if you are at the first result:
$counter = 0;
while ($row = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>';
if ( $counter == 0 ) {
echo '<br /><img src="images.php?id='. $n_id .'>';
}
}

Related

Call to a member function getTimestamp() on boolean

I got this error whatever format i changed, i have no idea what else causes of this. Can someone help me on this? My csv excel dateTime format is yyyy/mm/dd H:mm
$i = 0;
$len = count($map_column);
$insData = array();
foreach ($map_column as $key => $value) {
$i++;
if ($key == 'dateTime') {
$date = date_create_from_format('Y/m/d H:i:s', $row[$value]);
$timestamp = $date->getTimestamp();
$sql .= 'UNIX_TIMESTAMP('.$key . ') = "' . $timestamp . '"';
} else {
$sql .= $key . ' = "' . $row[$value] . '"';
}
$insData[$key] = $row[$value];
if ($i < $len) {
$sql .= ' AND ';
}
}
$result = mysqli_query($cons, $sql);
$r = mysqli_fetch_array($result);
$count = (int)$r['count'];
if ($count) {
return;
}
// pr($insData);
$columns = implode(", ",array_keys($insData));
$escaped_values = array_values($insData);
$values = implode("', '", $escaped_values);
$sql = "INSERT INTO hrar($columns) VALUES ('$values')";
if ($cons->query($sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}

Im having problems with nested forms

Hello im having some problems with my forms. It says "Saw a form start tag, but there was already an active form element. Nested forms are not allowed." but when im looking in my code the forms are not even nested in my code can i have some help.
$sql8 = 'SELECT läggtill.serier, läggtill.id, läggtill.id2, läggtill.säsonger, läggtill.betyg, kategorier.kategori from läggtill inner join'
. ' kategorier on läggtill.kategorier=kategorier.id order by läggtill.serier';
$resultat8 = $mysqli->query($sql8);
$antal8 = $resultat8->num_rows;
while ($rad8 = $resultat8->fetch_assoc()) {
echo "<li>" .$rad8['serier'] .', ' .$rad8['säsonger'] .' Säsonger, ' .$rad8['kategori'] .'<br>'.
'Betyg: ' .$rad8['betyg'] .'/10 </li>' .'<br>';
$id = $rad8['id'];
?>
<?php
echo '<form action="alla.php" method="POST">
<button name=' .$id .'>Ta bort</button>
</form>'
?>
<?php
if (isset($_POST[$id])) {
$sql9 = "Delete from läggtill where id=" . $id . " LIMIT 1";
if ($resultat9 === $mysqli->query($sql9)) {
header('location: alla.php');
} else {
echo "det misslyckades.";
}
echo "asdasd";
}
$id2 = $rad8['id2'];
echo "<div class=" . 'Update' . '>'
?>
<?php
echo "<form action='alla.php' method='POST'>
<label for='serier'>Serie: </label>
<input type='text' name='serier' id='serier'><br>";
$sql14 = 'SELECT * FROM kategorier';
$resultat14 = $mysqli->query($sql14);
$antal14 = $resultat14->num_rows;
if ($antal14 == 0) {
echo 'Inget funnet';
} else {
$sql15 = 'SELECT kategori, id FROM kategorier';
$resultat15 = $mysqli->query($sql15);
$antal15 = $resultat15->num_rows;
if ($antal15 == 0) {
echo 'Inget kategori funnen';
} else {
echo 'Kategori: <select name =kategorier>';
while ($rad15 = $resultat15->fetch_assoc()) {
echo '<option value='. $rad15['id'] . '>' . $rad15['kategori'] . '</option><br>';
}
echo '</select>';
}
}
echo '<br><button name= . $id2' . '>Uppdatera</button>';
if (isset($_POST[$id2])) {
$kategorier = $_POST['kategorier'];
$betyg = $_POST['betyg'];
$serier = $_POST['serier'];
$säsonger = $_POST['säsonger'];
if(strlen($kategorier) && strlen($säsonger) && strlen($serier) && strlen($betyg)) {
$sql12 = '"UPDATE läggtill SET kategorier="" . $kategorier . "", säsonger="" . $säsonger . "", serier="" . $serier . "","
. " betyg="" . $betyg . "" WHERE id2="" . $id2 . "";';
if($resultat9 == $mysqli->query($sql12)) {
header('location: alla.php');
} else {
echo 'Det misslyckades';
}
}
}
}
echo "</form>";
echo "</div>";`**enter code here**`
check have updated few things, hope it will work out
<?php
$sql8 = 'SELECT läggtill.serier, läggtill.id, läggtill.id2, läggtill.säsonger, läggtill.betyg, kategorier.kategori from läggtill inner join'
. ' kategorier on läggtill.kategorier=kategorier.id order by läggtill.serier';
$resultat8 = $mysqli->query($sql8);
$antal8 = $resultat8->num_rows;
while ($rad8 = $resultat8->fetch_assoc()) {
echo "<li>" . $rad8['serier'] . ', ' . $rad8['säsonger'] . ' Säsonger, ' . $rad8['kategori'] . '<br>' .
'Betyg: ' . $rad8['betyg'] . '/10 </li>' . '<br>';
$id = $rad8['id'];
?>
<?php
echo '<form action="alla.php" method="POST">
<button name=' . $id . '>Ta bort</button>
</form>'
?>
<?php
if (isset($_POST[$id])) {
$sql9 = "Delete from läggtill where id=" . $id . " LIMIT 1";
if ($resultat9 === $mysqli->query($sql9)) {
header('location: alla.php');
} else {
echo "det misslyckades.";
}
echo "asdasd";
}
$id2 = $rad8['id2'];
echo "<div class=" . 'Update' . '>'
?>
<?php
echo "<form action='alla.php' method='POST'>
<label for='serier'>Serie: </label>
<input type='text' name='serier' id='serier'><br>";
$sql14 = 'SELECT * FROM kategorier';
$resultat14 = $mysqli->query($sql14);
$antal14 = $resultat14->num_rows;
if ($antal14 == 0) {
echo 'Inget funnet';
} else {
$sql15 = 'SELECT kategori, id FROM kategorier';
$resultat15 = $mysqli->query($sql15);
$antal15 = $resultat15->num_rows;
if ($antal15 == 0) {
echo 'Inget kategori funnen';
} else {
echo 'Kategori: <select name =kategorier>';
while ($rad15 = $resultat15->fetch_assoc()) {
echo '<option value=' . $rad15['id'] . '>' . $rad15['kategori'] . '</option><br>';
}
echo '</select>';
}
}
echo '<br><button name= . $id2' . '>Uppdatera</button></form>';
if (isset($_POST[$id2])) {
$kategorier = $_POST['kategorier'];
$betyg = $_POST['betyg'];
$serier = $_POST['serier'];
$säsonger = $_POST['säsonger'];
if (strlen($kategorier) && strlen($säsonger) && strlen($serier) && strlen($betyg)) {
$sql12 = '"UPDATE läggtill SET kategorier="" . $kategorier . "", säsonger="" . $säsonger . "", serier="" . $serier . "","
. " betyg="" . $betyg . "" WHERE id2="" . $id2 . "";';
if ($resultat9 == $mysqli->query($sql12)) {
header('location: alla.php');
} else {
echo 'Det misslyckades';
}
}
}
}
echo "</div>";

HTML/PHP Form Submitting to 404 Eorror

SO. I work on this Wordpress site. Which believe me is SO annoying. So I have this huge mass of code which is a combo of PHP and HTML. Everything appears just fine until the form is submitted. When the page refreshes it loads to the same page (with the same exact URL) but with a 404 Error.
<center>
<code>
[insert_php]
// Info to connect to the Wishlist database
$servername = "eggcavity.com";
$dbusername = "******";
$password = "*******";
$dbname = "EggcavityTravelIndex";
try {
// To connect to the database please
$conn = new mysqli($servername, $dbusername, $password, $dbname);
if ($conn->connect_error) {
die('Connect Error (' . $conn->connect_errno . ') ' . $conn->connect_error);
}
// Get current user's username
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$allowed_roles = array('moderator', 'administrator');
if ($username == "") {
// If person is not logged in
echo "Please login to see if you have moderator privileges.";
} else if (array_intersect($allowed_roles, $current_user->roles )) {
// If person is logged in and has moderator privileges, show the page
$page = 1;
if($_SERVER['QUERY_STRING'] != ""){
$page = $_SERVER['QUERY_STRING'];
}
if(isset($_POST['submit'])){
$ids = $_POST['ids'];
$theCount = 0;
// Prepare and bind the udpate statement
$sql2 = "UPDATE Travels SET Name = ?, Picture = ?, Price = ?, " .
"Description = ?, Rarity = ?, Status = ? WHERE ID = ?";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param('ssssssi', $name, $picture, $price, $description, $rarity, $status, $id);
foreach($ids as $id){
$namecode = $id . "name";
$picturecode = $id . "picture";
$pricecode = $id . "price";
$raritycode = $id . "rarity";
$descriptioncode = $id . "description";
$statuscode = $id . "status";
$name = $_POST[$namecode];
$picture = $_POST[$picturecode];
$price = $_POST[$pricecode];
$rarity = $_POST[$raritycode];
$description = $_POST[$descriptioncode];
$status = $_POST[$statuscode];
$stmt2->execute();
}
if(isset($_POST['delete'])) {
$delete=!empty( $_POST['delete'] ) ? $_POST['delete'] : false;
if($delete && !is_array($delete) ){
$delete=explode(',',$delete);
}
foreach($delete as $id){
$sql = "DELETE FROM Travels WHERE ID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$theCount++;
}
}
echo "<font size='3'>The travel database has been updated, and " . $theCount .
" travels(s) has/have been removed.";
} else {
// Just display the form
$start = 0;
$stop = 100;
if ($page == "2") {
$start = 100;
$stop = 100;
} else if ($page == "3") {
$start = 200;
$stop = 100;
} else if ($page == "4") {
$start = 300;
$stop = 100;
} else if ($page == "5") {
$start = 400;
$stop = 100;
} else if ($page == "6") {
$start = 500;
$stop = 100;
} else if ($page == "7") {
$start = 600;
$stop = 100;
} else if ($page == "8") {
$start = 700;
$stop = 100;
} else if ($page == "9") {
$start = 800;
$stop = 100;
} else if ($page == "10") {
$start = 900;
$stop = 100;
} else if ($page == "11") {
$start = 1000;
$stop = 100;
} else if ($page == "12") {
$start = 1100;
$stop = 100;
} else if ($page == "13") {
$start = 1200;
$stop = 100;
} else if ($page == "14") {
$start = 1300;
$stop = 100;
} else if ($page == "15") {
$start = 1400;
$stop = 100;
}
$sql = 'SELECT * FROM Travels LIMIT ' . $start . ',' . $stop;
$result = mysqli_query($conn, $sql);
$sql11 = 'SELECT * FROM Travels';
$result11 = mysqli_query($conn, $sql11);
$numtravels = mysqli_num_rows($result11);
if ($numtravels > 0) {
$numpages = ceil($numtravels / 100);
$cnt = 1;
echo 'Pages: ';
while ($cnt <= $numpages){
echo '<a style="border-style:solid;border-radius:5px;border-width:1px;padding:2px;" href="?' . $cnt . '">'
. $cnt . '</a> ';
$cnt++;
}
echo '<br><br>';
$begin = $start + 1;
$end = $begin + 99;
echo '<form method="POST"><table style="border-collapse: collapse";>' .
'Travels ' . $begin . ' to ' . $end;
while($row = $result->fetch_assoc()) {
$name = $row["Name"];
$id = $row["ID"];
$picture = $row["Picture"];
$price = $row["Price"];
$rarity= $row["Rarity"];
$description = $row["Description"];
$status= $row["Status"];
echo '<tr>';
echo '<td rowspan="2" style="border:1px solid gray;text-align:center;"><br><img src="' . $picture . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;">' .
'<strong>Name: </strong><input type="text" name="' . $id . 'name" value="' . $name . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;">' .
'<strong>Image URL: </strong><input type="text" name="' . $id . 'picture" value="' . $picture . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;">' .
'<strong>Price: </strong><input type="text" name="' . $id . 'price" value="' . $price . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;">' .
'<strong>Rarity: </strong><input type="text" name="' . $id . 'rarity" value="' . $rarity . '"></td>';
echo '</tr>';
echo '<td colspan="2" style="border:1px solid gray;text-align:center;">' .
'<strong>Description: </strong><br><input style="width:300px" type="text" name="' . $id . 'description" value="' . $description . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;"><strong>Status: </strong><br><select name="' . $id . 'status">' .
'<option value ="' . $status . '" selected>' . $status . '</option>';
if ($status == "Available") {
echo '<option value ="Retired">Retired</option>';
} else if ($status == "Retired") {
echo '<option value ="Available">Available</option>';
} else if ($status == "") {
echo '<option value ="Retired">Retired</option>';
echo '<option value ="Available">Available</option>';
}
echo '</select></td>';
echo '<td style="border:1px solid gray;text-align:center;">' . '<strong>Delete</strong><br>' .
'<input type="checkbox" name="delete[]" value="' . $id . '"></td>';
echo '</tr>';
}
echo '</table><br><input name="submit" type="submit" id="submit" value="Update">' .
'</form><br><br>';
} else {
echo "<br>There are no travels in the database.";
}
}
} else {
// If person is logged in but does not have mod privileges
echo "Sorry, you're not a moderator!";
}
} catch (mysqli_sql_exception $e) {
throw $e;
}
// Close the connection to the database
$conn->close();
[/insert_php]
</code>
</center>
This is the form
And this is the page after submission.
The code is SO similar to a page that I do have working. As shown here:
<center>
<code>
[insert_php]
// Info to connect to the Wishlist database
$servername = "eggcavity.com";
$dbusername = "********";
$password = "*******";
$dbname = "EggcavityTravelIndex";
try {
// To connect to the database please
$conn = new mysqli($servername, $dbusername, $password, $dbname);
if ($conn->connect_error) {
die('Connect Error (' . $conn->connect_errno . ') ' . $conn->connect_error);
}
// Get current user's username
$current_user = wp_get_current_user();
$username = $current_user->user_login;
if ($username == ""){
echo "Please login to use wishlist feature.";
} else {
echo "<font size ='3'><br>Please click <strong>" .
"<a href = 'http://eggcavity.com/add-wishlist'>here</a></strong>" .
" to add creatures to your wishlist.<br><br>Please click <strong>" .
"<a href='http://eggcavity.com/wishlists'>here</a></strong> to view your wishlist.<br>" .
"<br>At the moment when you enter notes you cannot view them on the edit page." .
"<br>Please go to the view wishlist page to see your notes.<br><br></font>";
$page = 1;
if($_SERVER['QUERY_STRING'] != ""){
$page = $_SERVER['QUERY_STRING'];
}
if(isset($_POST['submit'])){
$ids = $_POST['ids'];
$theCount = 0;
// Prepare and bind the udpate statement
$sql2 = "UPDATE Wishlists SET Picture = ?, Stage = ?, Gender = ?, " .
"Frozen = ?, Notes= ? WHERE ID = ?";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param('sssssi', $picture, $stage, $gender, $frozen, $notes, $id);
foreach($ids as $id){
$stagecode = $id . "stage";
$gendercode = $id . "gender";
$frozencode = $id . "frozen";
$notescode = $id . "notes";
$namecode = $id . "creature";
$stage = $_POST[$stagecode];
$Stage = $stage;
$gender = $_POST[$gendercode];
$frozen = $_POST[$frozencode];
$notes = $_POST[$notescode];
$name = $_POST[$namecode];
$sql1 = 'SELECT * FROM Creatures WHERE Name = "' . $name . '"';
$result = mysqli_query($conn, $sql1);
$row = $result->fetch_assoc();
$picture = $row["$stage"];
$stmt2->execute();
}
if(isset($_POST['delete'])) {
$delete=!empty( $_POST['delete'] ) ? $_POST['delete'] : false;
if($delete && !is_array($delete) ){
$delete=explode(',',$delete);
}
foreach($delete as $id){
$sql = "DELETE FROM Wishlists WHERE ID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$theCount++;
}
}
echo "<font size='3'>Your wishlist has been updated, and " . $theCount .
" creature(s) has/have been removed from your wishlist.<br><br>Please click <u>" .
"<strong><a href='http://eggcavity.com/edit-wishlist'>here</a></u></strong>" .
" to return to the edit page.</href><br><br>";
} else {
// Just display the form
$start = 0;
$stop = 100;
if ($page == "2") {
$start = 100;
$stop = 100;
} else if ($page == "3") {
$start = 200;
$stop = 100;
} else if ($page == "4") {
$start = 300;
$stop = 100;
} else if ($page == "5") {
$start = 400;
$stop = 100;
} else if ($page == "6") {
$start = 500;
$stop = 100;
} else if ($page == "7") {
$start = 600;
$stop = 100;
} else if ($page == "8") {
$start = 700;
$stop = 100;
} else if ($page == "9") {
$start = 800;
$stop = 100;
} else if ($page == "10") {
$start = 900;
$stop = 100;
} else if ($page == "11") {
$start = 1000;
$stop = 100;
} else if ($page == "12") {
$start = 1100;
$stop = 100;
} else if ($page == "13") {
$start = 1200;
$stop = 100;
} else if ($page == "14") {
$start = 1300;
$stop = 100;
} else if ($page == "15") {
$start = 1400;
$stop = 100;
}
$sql = 'SELECT * FROM Wishlists WHERE Username = "' . $username .
'" ORDER BY Creature LIMIT ' . $start . ',' . $stop;
$result = mysqli_query($conn, $sql);
$sql11 = 'SELECT * FROM Wishlists WHERE Username = "' . $username . '"';
$result11 = mysqli_query($conn, $sql11);
$numcreatures = mysqli_num_rows($result11);
if ($numcreatures > 0) {
$numpages = ceil($numcreatures / 100);
$cnt = 1;
echo 'Pages: ';
while ($cnt <= $numpages){
echo '<a style="border-style:solid;border-radius:5px;border-width:1px;padding:2px;" href="?' . $cnt . '">'
. $cnt . '</a> ';
$cnt++;
}
echo '<br><br>';
$begin = $start + 1;
$end = $begin + 99;
echo '<form method="POST"><table style="border-collapse: collapse";><strong>' .
'Creatures ' . $begin . ' to ' . $end .
'<tr>' .
'<td>' . $numcreatures . ' creatures</td>' .
'<td style="border:1px solid gray;text-align:center;">Creature</td>' .
'<td style="border:1px solid gray;text-align:center;">Stage</td>' .
'<td style="border:1px solid gray;text-align:center;">Gender</td>' .
'<td style="border:1px solid gray;text-align:center;">Frozen</td>' .
'</tr></strong>';
while($row = $result->fetch_assoc()) {
$creature = $row["Creature"];
$id = $row["ID"];
$picture = $row["Picture"];
$stage = $row["Stage"];
$sql3 = 'SELECT * FROM Creatures WHERE Name = "' . $creature . '"';
$result2 = mysqli_query($conn, $sql3);
$row2 = $result2->fetch_assoc();
echo '<input name="ids[]" type="hidden" value="' . $id . '">' .
'<input name="' . $id . 'creature" type="hidden" value="' . $creature . '">' .
'<tr>' .
'<td rowspan="2" style="border:1px solid gray;text-align:center;"><img src="' .
$picture . '"></td>' .
'<td style="border:1px solid gray;text-align:center;">' . $creature . '</td>' .
'<td style"border:1px solid gray;text-align:center;"><select name="' . $id .
'stage">' .
'<option value ="' . $stage . '" selected>' . $stage . '</option>' .
'<option value = "Stage1">Stage1(Egg)</option>' .
'<option value = "Stage2">Stage2</option>';
if ($row2["Stage3"] != "") {
echo '<option value = "Stage3">Stage3</option>';
}
if ($row2["Stage4"] != "") {
echo '<option value = "Stage4">Stage4</option>';
}
echo '</select></td>' .
'<td style="border:1px solid gray;text-align:center;"><select name="' . $id .
'gender">' .
'<option value ="' . $row["Gender"]. '" selected>' . $row["Gender"] . '</option>' .
'<option value = "Unspecified">Unspecified</option>' .
'<option value = "Female">Female</option>' .
'<option value = "Male">Male</option>' .
'</select></td>' .
'<td style="border:1px solid gray;text-align:center;"><select name="' . $id .
'frozen">' .
'<option value ="' . $row["Frozen"] . '" selected>' . $row["Frozen"] . '</option>' .
'<option value="Unspecified">Unspecified</option>' .
'<option value="Yes">Yes</option>' .
'<option value="No">No</option>' .
'</select></td>' .
'</tr>' .
'<tr>' .
'<td style="border:1px solid gray;text-align:center;" colspan="3">Notes: ' .
'<input type="text" name="' . $id .
'notes" value="' . $row["Notes"] .'"></td>' .
'<td style="border:1px solid gray;text-align:center;">' . 'Delete<br>' .
'<input type="checkbox" name="delete[]"
value="' . $id . '"></td>' .
'</tr>';
}
echo '</table><br><input name="submit" type="submit" id="submit" value="Update">' .
'</form><br><br>';
} else {
echo "<br>You have no creatures in your wishlist.";
}
}
}
} catch (mysqli_sql_exception $e) {
throw $e;
}
// Close the connection to the database
$conn->close();
[/insert_php]
</code>
</center>
If you have any ideas what could be causing this please help!
Is your page saved and published?
If not, that might be the issue why it's saying that page is not found.
Also, there is a better way to handle page number generation (or pagination) than the long if else statements you have written.
Please go over this: http://code.runnable.com/U8dzQWEzMxxqeQ_E/php-pagination-example-using-mysql-database-for-dbms
It would also be wise to have a custom Wordpress plugin where you would have all this logic instead of pages since anyone with Wordpress admin access would be able to change the code and you certainly wouldn't want that.

How to add link to mysql database record as a <html> href?

if($rezultat = $polaczenie->query($sql))
$ilu_userow = $rezultat->num_rows;
while($ilu_userow>0){
$wiersz = $rezultat->fetch_assoc();
$id = $wiersz['id'];
$like = $wiersz['likes'];
$price = $wiersz['price'];
echo '<div class="pozycja">
<span>
<h1><font color="blue"/> '
. $id . ' </h1></font><font color="red"/> '
. $like . ' </font><h2><strong><font color="green"/> '
. $price . '</h2></strong></font></span></div>';
$ilu_userow = $ilu_userow - 1;
}
This is my code, and - as in the topic - i want to create html adress to every row i get from my database, and the question is - how?
if($rezultat = $polaczenie->query($sql)) $ilu_userow = $rezultat->num_rows; while($ilu_userow>0){
$wiersz = $rezultat->fetch_assoc();
$id = $wiersz['id'];
$like = $wiersz['likes'];
$price = $wiersz['price'];
echo '<div class="pozycja">
<span>
<h1><font color="blue"/> '
. $id . ' </h1></font><font color="red"/> '
. $like . ' </font><h2><strong><font color="green"/> '
. $price . '</h2></strong></font></span>';
//added line start
echo ''.$id.'';
echo '</div>';
//added line end
$ilu_userow = $ilu_userow - 1; }

mysqli_fetch: returns one row

I am making a private message system and I'm using the mysqli_fetch() function in a while statement to return all the rows associated with the query. However, PHP only returns the last row in MYSQL.
Here is my code:
<?php
$Connect = new mysqli("localhost", "root", "", "Data");
error_reporting(E_ALL ^ E_NOTICE);
session_start();
$Val = $_POST['ID'];
$Get = 'SELECT * FROM CMessages WHERE PID="'.$Val.'"';
$Username = $_SESSION['Username'];
$Admin = $_SESSION['Admin'];
if($Result = $Connect->query($Get))
{
while($Row = $Result->fetch_assoc())
{
$User = $Row['Username'];
$Msg = $Row['Msg'];
$Date = $Row['Date'];
$ID = $Row['ID'];
if($User == $Username)
{
$MText['T'] = '<div id="Msg">' . $User . ' : ' . $Msg . ' - ' . $Date .' - <a class="TLink" href="MDelete.php?ID='.$ID.'">Delete</a></div>';
}
elseif(isset($Admin))
{
$MText['T'] = '<div id="Msg">' . $User . ' : ' . $Msg . ' - ' . $Date .' - <a class="TLink" href="MDelete.php?ID='.$ID.'">Delete</a></div>';
}
else
{
$MText['T'] = '<div id="Msg">' . $User . ' : ' . $Msg . ' - ' . $Date .'</div>';
}
}
}
echo json_encode($MText);
?>
It returns all rows but , in the while loop, you are always overwriting the $MText variable. Therefore only the last one will be displayed with json_encode.
Maybe you meant to write $MText['T'][] instead of $MText['T'].

Categories