[PHP] Is this code right?

Status
Not open for further replies.
Newbie Spellweaver
Joined
Mar 17, 2013
Messages
20
Reaction score
1
Code:
<?PHP

$p1 = 5;


$pages = mysql_query("SELECT COUNT(id) FROM cms_comments");
$p2 = ceil(mysql_result($pages, 0) / $p1);


$page = (isset($_GET['page'])) ? (int)$_GET['page'] :  1;
$start = ($page - 1) * $p1;




$uids = $_SESSION['user']['id'];
$id = preg_replace('/\D/', '', $_GET['id']);


function getPost($uid) {
	$sql = "SELECT * FROM cms_comments WHERE author = '$uid' LIMIT $start, $p1";
	$query = mysql_query($sql) or die(mysql_error());
$nummy = mysql_query("SELECT * FROM cms_comments");
$numrows = mysql_num_rows($nummy);
	if(mysql_num_rows($query) > 0 && $_SESSION['user']['rank'] < 0) {
		return true;
	}
	else { return false; }
}
if(getPost($uids) == true) { echo 'You may only post once!'; }
function post($data, $story, $uid) {
	$clean = htmlentities(mysql_real_escape_string($data));
	$sql = "INSERT INTO cms_comments (story, comment, date, author) VALUES ('$story', '$clean', '". time() ."', '$uid')";
	$query = mysql_query($sql) or die(mysql_error());
}
$posts = getPost($uids);
if(isset($_POST['comment']) && strlen($_POST['comment']) > 0 && strlen($_POST['comment']) < 500 && $posts == false) {
	post($_POST['comment'], $id, $uids);
	echo '<head><meta http-equiv="refresh" content="0"></head>';
}






  	$sql = "SELECT * FROM cms_comments WHERE story = '{$id}' ORDER BY id DESC";
	$query = mysql_query($sql) or die(mysql_error());


while($ua = mysql_fetch_array($query)) {
	$uiq = mysql_query("SELECT * FROM users WHERE id = ". $ua['author']) or die(mysql_error());
	$ui = mysql_fetch_array($uiq) or die(mysql_error());
	
if($ui['rank'] >= 10) { $username = '<span style="font-family:Arial,Helvetica,sans-serif; color: #7D0A79;text-shadow: 0px 0px 5px #343434;background:url(/app/tpl/skins/Habbo/images/glitter.gif);">'. $ui['username'] .'</span>';}
else { $username = '<span style="font-family:Arial,Helvetica,sans-serif;">'. $ui['username'] .'</span>'; }


if($ui['rank'] >= 6) { $username = '<span style="font-family:Arial,Helvetica,sans-serif; color: #7D0A79;text-shadow: 0px 0px 5px #343434;background:url(/app/tpl/skins/Custom-Habbo/images/glitter.gif);">'. $ui['username'] .'</span>';}
else { $username = '<span style="font-family:Arial,Helvetica,sans-serif;">'. $ui['username'] .'</span>'; }
	}
echo "Pages: ";
if($p2 >= 1 && $page <= $p2){
for($x=1; $x<=$p2; $x++) {


echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
}
}


?>

I've been working on this code to paginate my comments system and sadly it's stuck in a syntax error. I can't find it. I'm new to coding , hope you guys can help me out
 
Theres something wrong with your query.

I think this one:
$sql = "SELECT * FROM cms_comments WHERE author = '$uid' LIMIT $start, $p1";

fix:
$sql = "SELECT * FROM cms_comments WHERE author = '".$uid."' LIMIT $start, $p1";

and:
$sql = "SELECT * FROM cms_comments WHERE story = '{$id}' ORDER BY id DESC";

fix:
$sql = "SELECT * FROM cms_comments WHERE story = '".$id."' ORDER BY id DESC";

Theres more, but not much time.
 
Upvote 0
Theres something wrong with your query.

I think this one:
$sql = "SELECT * FROM cms_comments WHERE author = '$uid' LIMIT $start, $p1";

fix:
$sql = "SELECT * FROM cms_comments WHERE author = '".$uid."' LIMIT $start, $p1";

and:
$sql = "SELECT * FROM cms_comments WHERE story = '{$id}' ORDER BY id DESC";

fix:
$sql = "SELECT * FROM cms_comments WHERE story = '".$id."' ORDER BY id DESC";

Theres more, but not much time.
Using those fixes , would it paginate?
 
Upvote 0
<?PHP
$p1 = 5;
$pages = mysql_query("SELECT COUNT(id) FROM `cms_comments`");
$p2 = ceil(mysql_result($pages, 0) / $p1);

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $p1;

$uids = $_SESSION['user']['id'];
$id = preg_replace('/\D/', '', $_GET['id']);

function getPost($uid) {
$sql = "SELECT * FROM `cms_comments` WHERE `author` = '".$uid."' LIMIT $start, $p1";
$query = mysql_query($sql) or die(mysql_error());
$nummy = mysql_query("SELECT * FROM `cms_comments`");
$numrows = mysql_num_rows($nummy);

if(mysql_num_rows($query) > 0 && $_SESSION['user']['rank'] < 0) {
return true;
}else{
return false;
}
}

if(getPost($uids) == true) {
echo 'You may only post once!';
}

function post($data, $story, $uid) {
$clean = htmlentities(mysql_real_escape_string($data));
$sql = "INSERT INTO `cms_comments` (`story`, `comment`, `date`, `author`) VALUES ('".$story."', '".$clean."', '".time()."', '".$uid."')";
$query = mysql_query($sql) or die(mysql_error());
}

$posts = getPost($uids);

if(isset($_POST['comment']) && strlen($_POST['comment']) > 0 && strlen($_POST['comment']) < 500 && $posts == false) {
post($_POST['comment'], $id, $uids);
echo '<head><meta http-equiv="refresh" content="0"></head>';
}

$sql = "SELECT * FROM `cms_comments` WHERE `story` = '".$id."' ORDER BY `id` DESC";
$query = mysql_query($sql) or die(mysql_error());

while($ua = mysql_fetch_array($query)) {
$uiq = mysql_query("SELECT * FROM `users` WHERE `id` = '".$ua['author']) or die(mysql_error());
$ui = mysql_fetch_array($uiq) or die(mysql_error());

if($ui['rank'] >= 10) { $username = '<span style="font-family:Arial,Helvetica,sans-serif; color: #7D0A79;text-shadow: 0px 0px 5px #343434;background:url(/app/tpl/skins/Habbo/images/glitter.gif);">'. $ui['username'] .'</span>';}
else { $username = '<span style="font-family:Arial,Helvetica,sans-serif;">'. $ui['username'] .'</span>'; }


if($ui['rank'] >= 6) { $username = '<span style="font-family:Arial,Helvetica,sans-serif; color: #7D0A79;text-shadow: 0px 0px 5px #343434;background:url(/app/tpl/skins/Custom-Habbo/images/glitter.gif);">'. $ui['username'] .'</span>';}
else { $username = '<span style="font-family:Arial,Helvetica,sans-serif;">'. $ui['username'] .'</span>'; }
}
echo "Pages: ";

if($p2 >= 1 && $page <= $p2) {
for($x=1; $x<=$p2; $x++) {
echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
}
}
?>

Try this (not tested anyway)
 
Upvote 0
Using those fixes , would it paginate?

That's not the problem at all. If you use double quote marks (") instead of single (') you can directly use PHP variables like that.
Break down the query and try which part fails, first use SELECT * FROM cms_comments, then add the WHERE clause and then the LIMIT clause etc. It would also help if you could post the PHP error, because that tells which line the error is. You can find the php error by adding error_reporting(E_ALL); on top of your file.
 
Upvote 0
Wait Isn't $ a variable just asking because I'm PHP noob... I thought that was a variable..

Verstuurd van mijn GT-I9070 met Tapatalk
 
Upvote 0
No never mind Azadi i was just wondering if
PHP:
 $
Is a variable.. I'm trying to learn PHP as much as i can.

Verstuurd van mijn GT-I9070 met Tapatalk
 
Upvote 0
Been working on another code

Code:
<?PHP$p1 = 3;
$pages = mysql_query("SELECT COUNT(id) FROM cms_comments");
$p2 = ceil(mysql_result($pages, 0) / $p1);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] :  0;
$start = ($page - 1) * $p1;
if(mysql_num_rows($getComments) == 0) {
    echo "No comments for this article!";
} else {
echo '<table width="528px">';
          while($Comments = mysql_fetch_array($getComments)){
          $getUserInfo = mysql_query("SELECT * FROM users WHERE username = '".$Comments['author']."'");
$userInfo = mysql_fetch_array($getUserInfo);
$nummy = mysql_query("SELECT * FROM cms_comments");
$numrows = mysql_num_rows($nummy);
                  echo '
                  <tr>
                    <td width="90px" valign="top"></div>
                    <div style="
height: 65px;
width: 50px;
float: left;
overflow: hidden;
">
                      <div style="float:left"><img position:absolute; src="http://habbo.fr/habbo-imaging/avatarimage?figure='.$userInfo['look'].'&size=b&direction=2&head_direction=3&gesture=sml&size=2"></div>
                      ';                                       
                echo '
                </td>
                    <td width="427px" valign="top">
                    <i><a href="/me">'.$userInfo['username'].' </a></i>
                                        <br /><br />'.$Comments['comment'].'                                   
                    </td>
                  </tr>
          <tr>
                    <td width="80px" valign="top">               
                    </td>
                                <td width="400px" align="right">                                                                                                           
</div></div></div></div>
<br>
<div style="width:125%; height:1px; background-color:#ccc; margin-top:-17px;"></div>
            </td>
          </tr>';
          }
          echo '</table>';
}
echo "Pages: ";
if($p2 >= 1 && $page <= $p2){
for($x=1; $x<=$p2; $x++) {
echo ($x == $page) ? '<strong><a href="' . $_SERVER['PHP_SELF'] . '?page='.$x.'">'.$x.'</a></strong> ' : '<a href="' . $_SERVER['PHP_SELF'] . '?page='.$x.'">'.$x.'</a> ';
}
}
?>

Problem with this code is , it shows the paginated pages , but doesn't display the comments
 
Upvote 0
And what is the difference between PDO OOP and normal PHP rather that OOP is clearly smaller....

Verstuurd van mijn GT-I9070 met Tapatalk
 
Upvote 0
OOP means Object Orientated Programming. If you dont know what it is, dont try to work with it. As you're beginning with PHP just do what you think is correct and if you run into trouble you can ask here. Later if you know the basics you can learn how to code in OOP.

PDO is just a way to handle mysql(i).

Usually PDO goes togheter with OOP.
 
Upvote 0
Well I've recently learned on Youtube from i guess PHPAcademy how to make your own kind-off CMS and that was coded in PDO as well so i personally think that was very nice made and stuff.

I'm already owning HTML . CSS But it's pretty hard to learn PhP.


Verstuurd van mijn GT-I9070 met Tapatalk
 
Upvote 0
OOP means Object Orientated Programming. If you dont know what it is, dont try to work with it. As you're beginning with PHP just do what you think is correct and if you run into trouble you can ask here. Later if you know the basics you can learn how to code in OOP.

PDO is just a way to handle mysql(i).

Usually PDO goes togheter with OOP.

Or you can also learn OOP and PHP together, it isn't that hard. Also you could've removed the (i). PDO and MySQLi are both 'updated' MySQL libraries, programmed itself in OOP.

@Topic:

I suggest you learn MySQLi because people can hack your hotel using MySQL exploits. Just giving a tip.
 
Upvote 0
These are tips that i will certainly use!

So what your implying is and please correct if making a mistake - I should just learn PHP And OOP together an just leave PDO out of it because PDO automatically programs itself in to OOP.

Oh and also learn MYSQI to avoid exploits.. Will do!

Verstuurd van mijn GT-I9070 met Tapatalk
 
Upvote 0
These are tips that i will certainly use!

So what your implying is and please correct if making a mistake - I should just learn PHP And OOP together an just leave PDO out of it because PDO automatically programs itself in to OOP.

Oh and also learn MYSQI to avoid exploits.. Will do!

Verstuurd van mijn GT-I9070 met Tapatalk

You should learn PHP and OOP together. You should also learn MySQLi/PDO (one of those 2) because they are better use for preventing exploiting.
 
Upvote 0
Status
Not open for further replies.
Back