I was working on my website and noticed that the <center> tab was not working correctly. I have an element wrapped in a <div id="main'> and a <center> tab and all of the rows of text don't line up correctly.
<?php
require('/inc.all.php');
$_SESSION['lastUrl'] = $_SERVER['REQUEST_URI'];
// Get total count of rows
$sql = "SELECT COUNT(username) FROM user_profile WHERE hideFromUserCount='0'";
$query = $db->query($sql);
$row = $query->fetch_row();
// Total row count
$rows = $row[0];
//Number of results per page
//if($rows < 10){
// $page_rows = $rows -1;
//}else{
$page_rows = 10;
//}
//Tells us number of last page
$last = ceil($rows/$page_rows);
if($last < 1)
$last = 1;
//Establish $pagenum variable
$pagenum = 1;
if(isset($_GET['pg']))
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pg']);
else
$pagenum = 1;
//Makes sure pagenumber isnt below 1 or more than $last
if($pagenum < 1)
$pagenum = 1;
else if($pagenum > $last)
$pagenum = $last;
//Sets range of rows for chosen $pagenum
$limit = 'LIMIT '.($pagenum-1) * $page_rows.','.$page_rows;
$sql = "SELECT username FROM user_profile WHERE hideFromUserCount='0' ORDER BY username ASC $limit";
$query = $db->query($sql);
$show_start = $pagenum * $page_rows;
$show_end = (($pagenum * $page_rows) + $page_rows)-1;
$textline1 = "Total Profiles: <b>$rows</b>";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = '';
if($pagenum > 1){
$previous = $pagenum-1;
$paginationCtrls .= "<li>Prev</li>";
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= "<li>$i</li>";
}
}
}else{
$previous = $pagenum-1;
$paginationCtrls .= "<li class='disabled'>Prev</li>";
}
$paginationCtrls .= "<li class='disabled'>$pagenum</li>";
for($i = $pagenum+1; $i<=$last; $i++){
$paginationCtrls .= "<li>$i</li>";
if($i >= $pagenum+4){
break;
}
}
if($pagenum != $last){
$next = $pagenum +1;
$paginationCtrls .= "<li>Next</li>";
}else{
$next = $pagenum +1;
$paginationCtrls .="<li class=\"disabled\">Next</li>";
}
$usernames = "";
while($row = $query->fetch_assoc()){
$username = $row['username'];
$usernames .="<br>$username<br>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Minecraft Profiles | Profiles</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style/css/style_viewProfile.css">
<link href="style/css/bootstrap.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<a class="brand" href="index">Minecraft Profiles</a>
<ul class="nav">
<li>Home</li>
<li class="active">Profiles</li>
<li>Find User</li>
<li>Create a Profile</li>
<li>User Dashboard</li>
<?php
if(isset($_SESSION['username']) && isset($_SESSION['password'])){
echo "<li> Welcome Back, ".$_SESSION['username']."! Click to log out</li>";
}else{
echo "<li>Login</li>";
}
?>
</ul>
</div>
</div>-->
<br />
<br />
<div id="header">
<h1>Profiles</h1>
</div>
</div>
<div id="content">
<div id="sidebar">
<strong>Featured Profiles</strong>
<br />
<?php
$sql = "SELECT username FROM user_profile WHERE featured = 1";
$query = $db->query($sql);
while($row = $query->fetch_assoc()){
$username = $row['username'];
echo "$username";
echo "<br />";
}
?>
</div>
<div id="main">
<center>
<?php echo $textline1;?><br />
<?php echo $textline2; ?><br />
<?php if(isset($usernames)) {echo $usernames; }?><br />
<div class="pagination"><ul><?php echo $paginationCtrls; ?></ul></div></center>
</div>
<div class="clear">
<?php
$sql="SELECT * FROM user_profile WHERE hideFromUserCount = 0";
$query=$db->query($sql);
echo "<div class=\"clear\"><br /><center>Minecraft Profiles is proud to have ".$query->num_rows." unique profile(s) registered in our databases!</center></div>";
?>
</div>
</div>
</body>
</html>
$textline1 and $textline2 just show some text on the screen. The end result looks like This
Have you tried removing the <center> altogether and styling the div?
<div id="main" style="text-align: center;">
Also - you'll need #main to have a width so CSS knows how wide to centre the text across
Related
We want to display a link of a list from one table in database‘X’.
Here is my code:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "root";
$password = "mysql";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("nentholbenin", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM utilisateurs ");
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>Categories</th>
</tr>
</thead>
<tbody>
<?php
$db_result = mysql_query("SELECT Categories FROM utilisateurs");
$result = $db_result; echo '<ul>';
foreach($array as $index => $db_result){
echo '<li><a href=".'$db_result['Categories'].'"</a></li>';
}
echo '</ul>';
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
</body>
</html>
I get this error:
Parse error: syntax error, unexpected '$db_result' (T_VARIABLE),
expecting ',' or ';' in
Try this:
<?php
$db_result = mysql_query("SELECT Categories FROM utilisateurs");
$result = $db_result; echo '<ul>';
foreach($array as $index => $db_result){
echo '<li><a href="'.$db_result['Categories'].'"</a></li>';
}
echo '</ul>';
?>
You forgot dot before $db_result.
And you can't directly get the data after executing the query using mysql_query. use mysql_fetch_array or mysql_fetch_assoc
try below code
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "root";
$password = "mysql";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("nentholbenin", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM utilisateurs ");
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>Categories</th>
</tr>
</thead>
<tbody>
<?php
$db_result = mysql_query("SELECT Categories FROM utilisateurs");
$result = $db_result; echo '<ul>';
while($array = mysql_fetch_assoc($result)) {
echo '<li>'.$array['Categories'].'</li>';
}
echo '</ul>';
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
</body>
</html>
I'm doing an assignment where I need to be able to change the color of ones log in page according to preference. Now I have been able to successfully do that BUT no matter what I do the color will not take up the whole page only around the FORM itself leaving the rest white, can you please tell me where I have made the mistake? Please see my coding below:
<?php
include('session.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
</head>
<body>
<div id="profile">
<b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
<b id="logout">Log Out</b>
<?php
$red = "";
$blue = "";
$green = "";
$gold = "";
$silver = "";
$purple = "";
$hour = time() + 3600;
if (isset($_POST['order']))
{
$color = $_POST['order'];
$$color = " selected";
setcookie("Free_cookies", $color, $hour);
}
else if(isset($_COOKIE['Free_cookies']))
{
$color = $_COOKIE['Free_cookies'];
$$color = " selected";
}
else
{
$color = "red";
$red = " selected";
}
$red = "";
$blue = "";
$green = "";
$gold = "";
$silver = "";
$purple = "";
$hour = time() + 3600;
// first check for a new value, and use it as well as saving it for next time
if (isset($_POST['order']))
{
$color = $_POST['order'];
$$color = " selected";
setcookie("Free_cookies", $color, $hour);
}
// if there's no new value, THEN check for a previous value in a cookie
else if(isset($_COOKIE['Free_cookies']))
{
$color = $_COOKIE['Free_cookies'];
$$color = " selected";
}
// otherwise default to red
else
{
$color = "red";
$red = " selected";
}
?>
<form method='post' <?php echo "STYLE='background-color:".$color.";'";?> ><p id='txtorder' >color: </p>
<select name='order' id='order'>
<option value="red" <?php echo $red; ?> >red</option>
<option value="blue" <?php echo $blue; ?> >blue</option>
<option value="green" <?php echo $green; ?> >green</option>
<option value="gold" <?php echo $gold; ?> >gold</option>
<option value="silver" <?php echo $silver; ?> >silver</option>
<option value="purple" <?php echo $purple; ?> >purple</option>
</select>
<input type='submit' value='sort'/>
</form>
</div>
</body>
</html>
A search result with pagination:
I have created this for my database search results with pagination
<?php
$con = mysql_connect("server","some_user","password"); // Enter hostname,user,password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// select database
mysql_select_db("some_db", $con);
$output = '';
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
//pagination code
$query = mysql_query("SELECT * FROM some_table WHERE Title Like '%$searchq%'"); //Counting total number of rows in the table 'data',
$total_rows = mysql_num_rows($query);
// setup configuration//
//step:3
$base_url = 'http://localhost/php_search/New/index.php'; //Provide location of you index file
$per_page = 1; //number of results to shown per page
$num_links = 8; // how many links you want to show
$total_rows = $total_rows;
$cur_page = 1; // set default current page to 1
//now we will extract information from url//
//step:4
if(isset($_GET['search']))
{
$cur_page = $_GET['search'];
$cur_page = ($cur_page < 1)? 1 : $cur_page; //if page no. in url is less then 1 or -ve
}
// calculate limit and offset, it'll will be used for Sql Query//
//step:5
$offset = ($cur_page-1)*$per_page; //setting offset
$pages = ceil($total_rows/$per_page); // no of page to be created
//Calculate the start and end page numbers for pagination links//
//step:6
$start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
$end = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;
//query the database with calculated OFFSET //
//step:7
$res = mysql_query("SELECT * FROM some_table WHERE Title Like '%$searchq%' LIMIT ".$per_page." OFFSET ".$offset);
mysql_close($con);
//pagination code
if(isset($res))
{
while($result = mysql_fetch_array($res))
{
$title = $result['Title'];
$name = $result['name'];
$description = $result['Description'];
$output .='<div><h1>'.$title.'<br>'.$name.'</h1><br>'.$description.'</div>';
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="Search here" />
<input type="submit" value="search" />
</form>
<?php print("$output"); ?>
<div id="pagination">
<div id="pagiCount">
<?php
if(isset($pages))
{
if($pages > 1)
{ if($cur_page > $num_links) // for taking to page 1 //
{ $dir = "first";
echo '<span id="prev"> '.$dir.' </span>';
}
if($cur_page > 1)
{
$dir = "prev";
echo '<span id="prev"> '.$dir.' </span>';
}
for($x=$start ; $x<=$end ;$x++)
{
echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':''.$x.' ';
}
if($cur_page < $pages )
{ $dir = "next";
echo '<span id="next"> '.$dir.' </span>';
}
if($cur_page < ($pages-$num_links) )
{ $dir = "last";
echo ''.$dir.' ';
}
}
}
?>
</div>
</div>
</body>
</html>
I have posted this code with which i am not getting any result on choosing another page
The pagination do occurs but on clicking to the second page there is no search result
Any help please
you pass the page number as "search" parameter
?search='.($cur_page+1)
but collect data only when $_POST['search'] is set
if(isset($_POST['search'])) {
so when you click a page link, you only have $_GET['search'] set, not $_POST['search']
I'm trying to display a list of titles in a modify/delete page. They load fine since I get the correct amount of rows, put for some reason I can't the text printed.
Here's the function which corresponds to the issue.
function manage_content()
{
echo '<div id="manage">';
$sql = "SELECT * FROM content";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)): ?>
<div>
<h2 class="title"><?php $row['title'] ?></h2>
<span class="actions">Edit | Delete</span>
</div> <?php
endwhile;
echo '</div>'; //End of Manage Div
}
And this is the full code:
<?php
class blog {
private $host;
private $username;
private $password;
private $db;
private $link;
public function __construct($host, $username, $password, $db){
$this->db = $db;
$this->link = mysql_connect($host, $username, $password, $db);
mysql_select_db($this->db, $this->link) or die (mysql_error());
}
function get_content($id=''){
if($id !=NULL):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE id = '$id'";
$return = '<p>Vover al Indice</p>';
else:
$sql = "SELECT * FROM content ORDER by id DESC";
endif;
$res = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($res) !=NULL):
while($row = mysql_fetch_assoc($res)){
echo '<h1>'.$row['title'].'</h1>';
echo '<p>'.$row['body'].'</p>';
}
else:
echo '<p>Oops, post not found!</p>';
endif;
if(isset($return)){
echo $return;
}
}
function add_content($p){
$title = mysql_real_escape_string($p['title']);
$body = mysql_real_escape_string($p['body']);
if(!$title OR !$body):
if(!$title):
echo "<p>You have to fill the title.</p>";
endif;
if(!$body):
echo "<p>You have to fill the body.</p>";
endif;
echo '<p>Try again!</p>';
else:
$sql = "INSERT INTO content VALUES (null, '$title', '$body')";
$res = mysql_query($sql) OR DIE (mysql_error());
echo "Added sucessfully!";
endif;
}
function manage_content()
{
echo '<div id="manage">';
$sql = "SELECT * FROM content";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)): ?>
<div>
<h2 class="title"><?php $row['title'] ?></h2>
<span class="actions">Edit | Delete</span>
</div> <?php
endwhile;
echo '</div>'; //End of Manage Div
}
}// End of Class
?>
You're forgetting an echo in your manage_content() function:
<?php $row['title'] ?>
Should be:
<?php echo $row['title']; ?>
That's part of the problem anyway.
I basically need to have take some videos information out of a database with a while loop and put them into a div. The only issue is that I need to put only 6 at a time in between a and tag and have it go to the next 6 and so forth. Here's my code:
$count = 0;
$sql = "SELECT * FROM videos ORDER BY id DESC";
$result_set = $database->query($sql);
while($videos = $database->fetch_array($result_set)) {
$count++;
// i know this is horribly wrong...
if($count == 0 || (($count % 6)+1 == 1)) {
echo '<div>';
}
// i need 6 videos to go in between the <div> and </div> tags then go on to another 6
echo "{$videos['title']}";
if($count == 0 || (($count % 6)+1 == 1)) {
echo '<div>';
}
}
This is an efficent way to do what you want:
$resultPerPage = 6;
$count = 0;
$sql = "SELECT * FROM videos ORDER BY id DESC";
$result_set = $database->query($sql);
$noPage = 1;
echo '<div id="page_1" class="pages">';
while($videos = $database->fetch_array($result_set)) {
$count++;
echo "{$videos['title']}";
if($count == $resultPerPage) {
echo '</div><div id="page_' . $noPage++ . '" class="pages">';
$count=0;
}
}
echo '</div>';