I tried to separate SQL's data with the following code:
$count = $pdo->query('SELECT COUNT(*) FROM tweets');
foreach ($count as $row) {
$num = $row[0];
echo 'count:'.$row[0].'<br>';
}
echo 'num value: '.$num.'<br>';
$max_page = ceil($num / 10);
echo 'page: '.$max_page.'<br>';
if (!isset($_GET['page'])) {
$_GET['page'] = 1;
} else {
$now = $_GET['page'];
echo '$_GET has been set successfully<br>';
}
$disp_start = $num - 10 * ($now - 1);
echo 'disp_start value: '.$disp_start.'<br>';
$disp_end = $num - 10 * $now;
echo 'disp_end value: '.$disp_end.'<br>';
$disp = $pdo->prepare('SELECT * FROM tweets ORDER BY id DESC LIMIT 10 OFFSET ?');
//$disp -> bindValue(':st', $disp_start);
//$disp -> bindValue(':en', $disp_end);
if ($disp->execute([$disp_end])) {
echo 'success!';
foreach ($disp as $row) {
echo '<div class="breakwater2">';
echo '<div class="tweet">';
echo '<a href="./user/'.$row['uploader'].'.php">';
echo '<img src="', $row['avatar'], '" class="avatar1">';
echo '<div class="cont">';
echo '<b class="username">', $row['uploader'], '</b></a>';
echo '<p class="contents1">', $row['contents'], '</p>';
echo '<p class="time">', $row['time'], '</p>';
echo '</div>';//cont
echo '</div>';//tweet
echo '<form action="" method="post"><input type="hidden" name="del" value='.$row['id'].'>';
if ($_SESSION['user']['id'] == $row['userid']) {
echo '<input type="submit" value="Delete">';
}
echo '</form>';
echo '</div>';
echo '<hr class="division">';
}
} else {
echo 'failure';
print_r ($disp -> errorInfo());
}
for ($i = 1; $i <= $max_page; $i++) {
if ($i == $now) {
echo $now;
} else {
echo ''.$i.'';
}
}
However, when I execute this code an error occurs and it says
failureArray ( [0] => 42000 [1] => 1064 [2] => 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 ''53'' at line 1
But, if I don't use a placeholder and substitute a value directly to the 'OFFSET' value, it works. How can I solve it?
Related
I have a 'map' kind of thing for something im working on. Im drawing each tile out and then checking against a database to see if somebody is located at that tile.
The code works but only for the first result in the db.
Can anyone help. many thanks.
$sqlw = "SELECT id, player_coord_x, player_coord_y FROM player_game WHERE world_id='$world'";
$world_result = $player_stat->query($sqlw);
?>
<div class='map-grid'>
<?
$id = '';
$size = 16;
for($i = 1; $i <= $size; $i++) {
echo "<div class='map-grid-row'>";
for ($j=1; $j <= $size; $j++) {
// check for player at location
if ($world_result->num_rows > 0) {
while($w_row = $world_result->fetch_assoc()) {
$player_coord_x = $w_row['player_coord_x'];
$player_coord_y = $w_row['player_coord_y'];
$id = $w_row['id'];
}
}
if ($player_coord_x == $i and $player_coord_y == $j){
echo "<div class='map-grid-cell high'>";
echo "XXX";
echo "</div>";
}else{
echo "<div class='map-grid-cell high'>";
echo "<span class=\"map-small\">(x-$i y-$j)</span>";
echo "</div>";
}
}
echo "</div>";
}
?>
</div>
$sqlw = "SELECT id, player_coord_x, player_coord_y FROM player_game WHERE world_id='$world'";
$world_result = $player_stat->query($sqlw);
if($world_result->num_rows > 0){
while($w_row = $world_result->fetch_assoc()){
//first type of array
$player[] = array(
'x' => $w_row['player_coord_x'], //your player X record
'y' => $w_row['player_coord_y'], //your player Y record
'id' => $w_row['id'] //your player id
);
//second type of array
// OR store player's X and Y as key of array
$player[$w_row['player_coord_x'] . '-' . $w_row['player_coord_y']] = $w_row['id'];
// This are only if the player record will never repeat
}
}
?>
<div class="map-grid">
<?php
$size = 16;
for($i = 1; $i <= $size; $i++){
echo '<div class="map-grid-row">';
for($j = 1; $j <= $size; $j++){
//with first type of array
$exists = false;
foreach($player as $play){
if($play['x'] == $i && $play['y'] == $j){
$exists = true;
break; // break the loop once you got the result.
}
}
if($exists){ //Player exists? if no detect in foreach loop above, default exists would return false.
echo '<div class="map-grid-cell high">';
echo 'XXX';
echo '</div>';
} else {
echo '<div class="map-grid-cell high">';
echo '<span class="map-small">(x-' . $i . ' y-' . $j . ')</span>';
echo '</div>';
}
//second type of array
//Check if the key exists and if it's empty.
if(isset($player[$i.'-'.$j]) && !empty($player[$i.'-'.$j])){
echo '<div class="map-grid-cell high">';
echo 'XXX';
echo '</div>';
} else {
echo '<div class="map-grid-cell high">';
echo '<span class="map-small">(x-' . $i . ' y-' . $j . ')</span>';
echo '</div>';
}
}
}
?>
</div>
First of all my English language is not perfect so try to explain my problem at my best. So i have a problem with Invalid argument supplied for foreach() and i have no idea what's wrong. I wanted to create a comment system where i can put a reginstered users. If you have any ideas or working comment system where i could use my data base with users i would be grateful.
Here is my code
<?php
function get_comments($file_id) {
require 'polaczenie.php';
$result = "SELECT * FROM `comments` WHERE `file_id`='$file_id' AND `is_child`=FALSE ORDER BY `date` DESC";
$row_cnt =mysql_query($result);
echo '<h1>Comments ('.$row_cnt.')</h1>';
echo '<div class="comment">';
new_comment();
echo '</div>';
foreach($result as $item) {
$date = new dateTime($item['date']);
$date = date_format($date, 'M j, Y | H:i:s');
$auth = $item['author'];
$par_code = $item['com_code'];
$chi_result ="SELECT * FROM `comments` WHERE `par_code`='$par_code' AND `is_child`=TRUE";
$chi_cnt = mysql_query($chi_result);
echo '<div class="comment" name="'.$item['com_code'].'">'
.'<span class="author">'.$auth.'</span><br />'
.$item['comment'].'<br />'
.'<span class="date">Posted: '.$date.'</span><br />';
if($chi_cnt == 0) {
echo '<span class="replies">No replies</span>'
.'<span class="replies"> Reply</span>';
} else {
echo '<span class="replies">[+] '.$chi_cnt.' replies</span>'
.'<span class="replies" Reply</span>';
add_comment($item['author'], $item['com_code']);
echo '<div name="children" id="children">';
foreach($chi_result as $com) {
$chi_date = new dateTime($com['date']);
$chi_date = date_format($chi_date, 'M j, Y | H:i:s');
echo '<div class="child" name="'.$com['com_code'].'">'
.'<span class="author">'.$com['author'].'</span><br />'
.$com['comment'].'<br />'
.'<span class="date">Posted: '.$chi_date.'</span><br />'
.'</div>';
}
echo '</div>';
}
echo '</div>';
}
mysql_close($conn);
}
function add_comment($reply, $code) {
echo '<form action="reply.php" method="post" enctpye="" name="new_comment">'
.'<input type="hidden" name="par_code" value="'.$code.'" />'
.'<textarea class="text_cmt" name="text_cmt" placeholder="Reply to '.$reply.'"></textarea><br />'
.'<input type="submit" value="Reply" />'
.'</form>';
}
function new_comment() {
echo '<form action="new.php" method="post" enctpye="" name="new_comment">'
.'<textarea class="text_cmt" name="text_cmt" placeholder="Post a new comment"></textarea><br />'
.'<input type="submit" value="Post" />'
.'</form>';
}
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$characterLength = strlen($characters);
$randomString = '';
for($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $characterLength - 1)];
}
return $randomString;
}
function checkString($com_code) {
include 'database.php';
$rand = generateRandomString();
$result ="SELECT * FROM `comments` WHERE `com_code`='$com_code'";
$row_cnt = mysql_query($result);
if($row_cnt != 0) {
return $rand;
} else {
checkString($rand);
}
}
?>
Probably
foreach($result as $item) {
should be
foreach($row_cnt as $item) {
You are passing to the foreach function the query string.
Here, $result is just a variable containing your query. foreach loop requires an array or object which it can traverse. You should use while loop instead.
Example Scenario:
$result = "SELECT * FROM `comments` WHERE `file_id`='$file_id' AND `is_child`=FALSE ORDER BY `date` DESC";
$comments = mysql_query($result);
while($comment = mysql_fetch_array($comments))
{
//Do whatever you want with your each comment
}
I hope this will help :)
Or you can follow steps from here W3schools mySql Select with php while loop
My website currently ignores the first two images you place into the database and then proceeds to add images going across 5 columns and then moving down to the next row.
Update: Now it shows 3 of the 4 images in the database. Skips one image.
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
This is what my database looks like
http://i.stack.imgur.com/IFba8.jpg
This is what my website shows
http://i.stack.imgur.com/Wf7E1.jpg
Try this:
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
Please try this.
<?php
$i = 1;
echo "<table>";
while ( $row = $Recordset2->fetch_object() ) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
So I have a function that takes a number and outputs it as a placement. How do I go about making the function take in consideration ties. I have a ranking database and this php code echos out rankings on a table. right now it ranks but it doesn't consider ties. How do i go about doing this
<?php
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}
?>
<?php
$link = mysqli_connect("localhost", "citricide", "321213123Lol", "juneausmashbros");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM rankings ORDER BY points DESC";
$result = mysqli_query($link, $query);
echo '<article class="content grid_6 push_3">';
echo '<h1>';
echo 'Project M Summer Ranbat Rankings';
echo '</h1>';
echo '<section>';
echo '<center>';
echo '<table style="width:400px" class="rankslist">';
echo '<tr>';
echo '<th width="15%"><b>Rank</b></th>';
echo '<th width="45%"><b>Name</b></th>';
echo '<th width="45%"><b>Alias</b></th>';
echo '<th width="15%"><b>Points</b></th>';
echo '</tr>';
$ass = 0;
while($row = $result->fetch_array()) {
$ass++;
echo '<tr>';
if ($ass == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($ass == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($ass == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($ass);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
echo '</table>';
echo '</center>';
echo '</section>';
echo '</article>';
?>
It seems like you need additional variables to track the rank and the previous value. By doing so, you can handle ties.
For example:
$ass = 0; // current record count
$rank = 0; // rank
$last_points = NULL; // variable to store last ranked value
while($row = $result->fetch_array()) {
$ass++;
// check if value changes and reset rank if it does
if ($row['points'] !== $last_points) {
$rank = $ass;
$last_points = $row['points'];
}
echo '<tr>';
if ($rank == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($rank == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($rank == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($rank);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
So say your points looked like this:
100
100
90
80
The $rank value would be:
1
1
3
4
CODE:
<?php
$page = $_GET['page'];
$category = $_GET['cat'];
$your_db = # new mysqli("database","name","password");
if (mysqli_connect_errno())
{
echo 'ERROR!
'.mysqli_connect_errno()
.' - Not connected : '.mysqli_connect_error().'
';
die;
}
else
{
$db_connect = $your_db->select_db("databasename");
if (!$db_connect)
{
echo 'ERROR CONNECT DATA BASE';
die;
}
}
echo '<p>';
$query = "select distinct fldCity from info order by fldCity";
$result = $your_db->query($query);
$number_of_records = $result->num_rows;
for ($i = 0; $i < $number_of_records; $i++)
{
$row = $result->fetch_assoc();
echo '<a href="index.php?cat='.stripslashes($row['fldCity']).'">';
echo stripslashes($row['fldCity']);
echo '</a> / ';
}
echo '</p>';
if ($category)
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where fldCity = '$category' and length(fldCompanyLogo) > 0 order by fldCity";
}
else
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where length(fldCompanyLogo) > 0 order by fldCity";
}
$result = $your_db->query($query);
$number_of_records = $result->num_rows;
$num_pages = $number_of_records / 7;
if (($number_of_records % 7) > 0 )
{
$num_pages++;
}
if (strlen($page) == 0)
{
$page = 0;
}
else
{
$page = $page * 4;
}
echo '<table border="0" cellspacing="10" cellpadding="10" style="border-collapse: collapse" bordercolor="#111111">';
$row_num = 4;
$result->data_seek($page);
for ($i = $page; $i < $number_of_records; $i++)
{
if ($row_num <= 4)
{
echo '<tr>';
for ($col_num = 0; $col_num < 4; $col_num++)
{
$row = $result->fetch_assoc();
echo '<td>';
show_image(stripslashes($row['fldCompanyLogo']),stripslashes($row['fldCompanyName']));
echo '
';
echo '<b><font face="Tahoma" size="2"><a href="mysite.ca/'.stripslashes($row['fldMenuLink']).'" target="_blank"></font>';
echo '<font face="Tahoma" size="2"><a href="'.stripslashes($row['fldMenuLink']).'" target="_blank">';
echo stripslashes($row['fldCompanyName']);
echo '</a>';
echo '
';
echo stripslashes($row['fldCity']);
echo '
';
echo stripslashes($row['fldProvince']);
echo '
';
echo stripslashes($row['fldPostalCode']);
echo '</td>';
}
$row_num++;
echo '</tr>';
}
else
{
break;
}
}
echo '</table>';
for ($j = 0; $j < $num_pages; $j++)
{
$page_link = $j + 1;
echo '<font face="Tahoma" size="4"><b>'.$page_link.'</b></font> ';
}
echo ' '.$number_of_records;
function show_image($image_name)
{
if (file_exists("'$image_name'"))
{
$dim_img = getimagesize('/images/'.$image_name);
echo '<img src="/images/'.$image_name.'" alt = '.$alt.' border=0 align="bottom"';
echo 'width = '. $dim_img[100] .' height = ' .$dim_img[100] . ' />';
}
else
echo 'Add your image here!';
}
?>
I am totally lost and I can't find the error to why the images from the database isn't loading with the script. I highlighted the area in blue where I believe the error is to be. I just can't find any errors in that particular spot! All I want is the images from a column I have in my database to be fetched and connected to 'echo '
No images are being displayed, and I can't find the error as to why the images aren't showing up
try Convert Image to Base64 String and Base64 String to Image
also why echo ''; you use this its makes no sense