I'm trying to add a counter to the table in the following code. but I couldn't be successful. Can I get a little help, please? thanks. Something like this:
$counter = 0;
$counter++;
if($counter % 33 == 0)
So that when the counter is added, table will continue after %33 on the right of the page, and it will continue like that, instead of going down the page.
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="etc/sumain.css" />
</head>
<body>
<table class="tbresult">
<?php
include ("confige.php");
$query = 'select * from employees';
$result = mysqli_query($link, $query);
if (!$result) {
$message = 'ERROR:' . mysqli_error($link);
return $message;
} else {
$i = 0;
echo '<form name="select" action="" method="GET">';
echo '<select name="mySelect" id="mySelect" onchange="this.form.submit()">';
while ($i < mysqli_field_count($link)) {
$meta =
mysqli_fetch_field_direct($result, $i);
echo '<option>' . $meta->name . '</option>';
$i = $i + 1;
}
echo '</select>';
echo '</form>';
}
if(isset($_GET['mySelect'])) {
$myselect = $_GET['mySelect'];
$sql = "SELECT `$myselect` as mySelect FROM employees"; // add column alias
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
}
echo "</table>";
}
}
mysqli_close($link);
?>
</body>
</html>
First, don't have an HTML form inside a table, this is not valid, AFIK, and can cause you many troubles in different browsers.
You need simply open the table once, then create a counter = 0 and on each while loop add 1 to it. Then check, if it divides by 33 than you close the table and open a new one. After the loop you close the last table.
The side by side alignment can be done with CSS, something like .tbresult {float: left; width: 200px;}
Something like this:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="etc/sumain.css" />
</head>
<body>
<?php
include ("confige.php");
$query = 'select * from employees';
$result = mysqli_query($link, $query);
if (!$result) {
$message = 'ERROR:' . mysqli_error($link);
return $message;
} else {
$i = 0;
echo '<form name="select" action="" method="GET">';
echo '<select name="mySelect" id="mySelect" onchange="this.form.submit()">';
while ($i < mysqli_field_count($link)) {
$meta =
mysqli_fetch_field_direct($result, $i);
echo '<option>' . $meta->name . '</option>';
$i = $i + 1;
}
echo '</select>';
echo '</form>';
}
if(isset($_GET['mySelect'])) {
$myselect = $_GET['mySelect'];
$sql = "SELECT `$myselect` as mySelect FROM employees"; // add column alias
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
// output data of each row
$table_row_counter = 0;
echo '<table class="tbresult">';
while($row = $result->fetch_assoc())
{
$table_row_counter++;
if ($table_row_counter % 33 == 0) {
echo '</table>';
echo '<table class="tbresult">';
}
echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
}
}
}
mysqli_close($link);
?>
</body>
</html>
Related
I have a 165 line php script that I would like to integrate into my html webpage.
The php script parses csv files uploaded by the user for column names. The html page allows the user to select variables from a dropdown menu. I want to let the php script run when a user uploads a file so the dropdown menu can be populated with the results from the php script.
this the html
<!DOCTYPE html>
<html lang="en">
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style type="text/css">
body {
padding-left: 11em;
}
ul.navbar {
position: absolute;
top: 2em;
left: 1em;
width: 9em }
h1 {
font-family: Helvetica, Geneva, Arial,
SunSans-Regular, sans-serif }
.float-child {
width: 50%;
float: left;
padding: 20px;
}
label {
cursor: pointer;
}
textarea {
width: 400px;
height: 150px;
}
</style>
<body>
<!-- Page content -->
<div class="w3-content" style="max-width:2000px;margin-top:46px">
<div class="w3-container w3-content w3-padding-64" style="max-width:800px" id="contact">
<h2 class="w3-wide w3-center">Create Crosstabs</h2>
<ul class="navbar">
<li>Upload Crosstab Data
<li>Create Crosstab
<li>View Crosstab
<li>View Past Crosstabs
</ul>
</div>
<div class="float-container">
<div class="float-child">
<div class="green">Choose Crosstab
<label for="cars">Variable for Column 1: </label>
<select name="cols" id="cols">
<option value="Age">Age</option>
<option value="Ethnicity">Ethnicity</option>
<option value="Income">Income</option>
</select>
<br>
<label for="cars">Variable for Column 2: </label>
<select name="cols" id="cols">
<option value="Age">Age</option>
<option value="Ethnicity">Ethnicity</option>
<option value="Income">Income</option>
</select>
<br>
<button id="1" onClick="reply_click(this.id)">Create</button>
</div>
</div>
<div class="float-child">
<div class="blue">Sucessfully Uploaded Records
<div>
<label for="input-file">Specify a file:</label><br>
<input type="file" id="input-file">
</div>
<textarea id="content-target"></textarea>
</div>
</div>
<h1><?php echo "This message is from server side." ?></h1>
<!-- End Page Content -->
</div>
<!-- Image of location/map -->
<img src="/w3images/map.jpg" class="w3-image w3-greyscale-min" style="width:100%">
<script>
// Automatic Slideshow - change image every 4 seconds
var myIndex = 0;
carousel();
function reply_click(clicked_id)
{
alert(clicked_id);
}
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 4000);
}
// Used to toggle the menu on small screens when clicking on the menu button
function myFunction() {
var x = document.getElementById("navDemo");
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
}
// When the user clicks anywhere outside of the modal, close it
var modal = document.getElementById('ticketModal');
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
document.getElementById('input-file')
.addEventListener('change', getFile)
function getFile(event) {
const input = event.target
if ('files' in input && input.files.length > 0) {
placeFileContent(
document.getElementById('content-target'),
input.files[0])
}
}
function placeFileContent(target, file) {
readFileContent(file).then(content => {
target.value = content
}).catch(error => console.log(error))
}
function readFileContent(file) {
const reader = new FileReader()
return new Promise((resolve, reject) => {
reader.onload = event => resolve(event.target.result)
reader.onerror = error => reject(error)
reader.readAsText(file)
})
}
</script>
</body>
</html>
and this is the php
<?php
//Connection
$db = pg_connect( "host=localhost dbname=dbname user=postgres password=password" );
if(!$db) {
echo "Error : Unable to open database";
} else {
echo "Opened database successfully";
echo "<br>";
}
//Using this to test queries
function testQuery($ret, $completedmsg=""){
if(!$ret){
echo pg_last_error();
echo "<br>";
}else{
echo $completedmsg;
echo "<br>";
}
}
//Added a break function, I was sick of adding breaks for echoing
function break_html(){
echo "<br>";
}
//Getting file
$CSV_File = "pi2sample5.csv";
$csv = file($CSV_File);
//Get type of each column (count(int), percentage(float), ""(varchar))
$type = explode(",", $csv[0]);
//Get each column name
$column_name = explode(",", $csv[1]);
//Remove .csv (lazy, but I will add a regex here)
$table_name = substr($CSV_File,0,-4);
$table_name = preg_replace('/\..*/', '', $CSV_File);
//Check if table exists
$checkExists = "SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename =" . "'" . $table_name . "'". ")";
$exists = False;
$ret = pg_query($db, $checkExists);
$i = 0;
while ($row = pg_fetch_row($ret))
{
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
if($c_row == 't'){
$exists = True;
}
next($row);
$y = $y + 1;
}
}
//If table exists, skip creation
if(!$exists){
//Create new Table
$sql = "CREATE TABLE IF NOT EXISTS " . $table_name . "()";
$ret = pg_query($db, $sql);
if(!$ret){
echo pg_last_error();
echo "<br>";
}else{
echo "CREATED NEW TABLE";
echo "<br>";
}
//Making columns with correct variable types and names
$i = 0;
while ($i < count($column_name)){
if ($type[$i] == "Count"){
$new_type = "INT";
}elseif ($type[$i] == "Dollars"){
$new_type = "INT";
}elseif ($type[$i] == "Percentage"){
$new_type = "float8";
}else{
$new_type = "VARCHAR(12)";
}
$new_column = preg_replace('/[^A-Za-z0-9]/', '', $column_name[$i]);
$i = $i+1;
$ret = pg_query($db, "ALTER TABLE " . $table_name . " ADD COLUMN " .$new_column. " " . $new_type);
if(!$ret){
echo pg_last_error();
echo "<br>";
}else{
echo "ADDED NEW COLUMN: " . $new_column;
echo "<br>";
}
}
//Copy file into a safe "previous" version
if (!copy($CSV_File, "old" . $CSV_File)) {
echo "failed to copy $file...\n";
}
//Remove first row of CSV (count, count, percentage, etc...)
$new_csv = array_splice($csv, 1);
file_put_contents($CSV_File, implode("", $new_csv));
$createCSVtable ="COPY " . $table_name . " FROM '/home/monty/PI2/PI2Team/" . $CSV_File . "' DELIMITER ',' CSV HEADER;";
file_put_contents("rewritten.csv", $old_csv);
$ret = pg_query($db, $createCSVtable);
testQuery($ret);
//Restore the file with removed row
if (!copy("old" . $CSV_File, $CSV_File)) {
echo "failed to copy $file...\n";
}
if(!unlink("old" . $CSV_File)){
echo "Couldn't delete old$CSV_File";
}
}
//GET VARIABLES/COLUMN NAMES
$ret = pg_query($db, "SELECT
column_name
FROM
information_schema.columns
WHERE table_name =" . "'" . $table_name . "'" );
if(!$ret){
echo pg_last_error($db);
} else{
echo "SHOWING TABLE";
//echo pg_fetch_row($ret);
}
echo '<html><body><select name="variables">';
//Putting variables (column names) into dropdown menu in html
$i = 1;
while ($row = pg_fetch_row($ret))
{
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo "<option value=" . "'" . $c_row . "'" . ">" . $c_row . "</option>";
next($row);
$y = $y + 1;
}
$i = $i + 1;
}
echo "</select>";
pg_free_result($ret);
pg_close($db);
?>
Just set up your web server so that HTML files parse through PHP. This varies depending on your server. Alternatively, just save the file as .php and it should run!
EDIT: Having issues with extra " or some sort of syntax when adding second select clause.
So im working on a php project using the mysql adventureworks sample database. Currently this page takes in an employees title which you can select from using the drop down box filled with all the possible titles to filter through the employees. However i wanted to add a second box with gender so someone could filter through a title and gender and get all the possible employees.
I have a tried a few things but they usually end up crashing the page.
Heres the first file: search.php
<?php
// Connect to the database
if (!include('connect.php')) {
die('error finding connect file');
}
$dbh = ConnectDB();
?>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Cuprum'
rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/css?family=Amaranth"
rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono"
rel="stylesheet">
<link href="main.css" rel="stylesheet">
<title>Table Results</title>
</head>
<body>
<div class="content">
<h1>Search Adventureworks Employees</h1>
<p>Select two parameters and search information from the
AdventureWorks Database.</p>
<div class="form">
<form action="listTable.php" method="get">
<?php
// Get the full list of titles from Employee Table
$sql = "SELECT Distinct title FROM adventureworks.employee";
$stmt = $dbh->prepare($sql);
$stmt->execute();
// Prep drop down control
echo "<label for='title'>Select title: </label>\n";
echo "<select id='title' name='title'>\n";
// Put titles in the options
foreach ($stmt->fetchAll() as $titles) {
echo "<option value='" . $titles['title'] . "'>" .
$titles['title'] . "</option>\n";
}
// End dropdown
echo "</select>";
// Get the full list of genders from employee table
$sql3 = "Select Distinct gender FROM adventureworks.employee";
$stmt = $dbh->prepare($sql3);
$stmt->execute();
//Prep dropdown
echo "<label for ='gender'>Select Gender: </label>\n";
echo "<select id='gender' name = 'gender'>\n";
// Put genders in the options
foreach($stmt->fetchAll() as $genders) {
echo "<option value ='" . $genders['gender'] . "'> .
$genders['gender'] . "</option>\n";
}
//end dropdown and submit
echo "</select> <input type='submit'
value='Submit'>\n</form>\n</div>";
?>
</div>
</body>
</html>
And heres the second php file: listTable.php
<?php
// Connect to the database
if (!include('connect.php')) {
die('error finding connect file');
}
$dbh = ConnectDB();
?>
<html>
<head><link href='http://fonts.googleapis.com/css?family=Cuprum'
rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/css?family=Amaranth"
rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono"
rel="stylesheet">
<link href="main.css" rel="stylesheet">
<title>Table Results</title>
</head>
<body>
<div class='content'>
<?php
// Get table name from querystring
if (!isset($_GET["title"])) {
echo "No Title Selected";
}
else {
$title=$_GET["title"];
echo "<h1>Listing of Employees</h1>\n";
$sql1 = "SELECT column_name FROM information_schema.columns ";
$sql1 .= "WHERE table_name = 'employee'";
$stmt = $dbh->prepare($sql1);
$stmt->execute();
$cols = $stmt->rowCount();
// Prep table
$tableHTML = "<table>\n<thead>\n<tr>\n";
// Table headings (column names)
foreach ($stmt->fetchAll() as $columns) {
$tableHTML .= "<th>" . $columns['column_name'] . "</th>\n";
}
// Prep table body
$tableHTML .= "</tr>\n</thead>\n<tbody>\n";
// Table body (column values)
$sql2 = "SELECT * FROM adventureworks.employee e";
$sql2 .= " Where title like '$title'";
$stmt = $dbh->prepare($sql2);
$stmt->execute();
echo $stmt->rowCount() . " rows retrieved<br/><br />\n";
foreach ($stmt->fetchAll() as $rows ) {
$tableHTML .= "<tr>\n";
for ($i = 0; $i < $cols; $i++) {
$tableHTML .= "<td>" . $rows[$i] . "</td>\n";
}
$tableHTML .= "</tr>\n";
}
// End table
$tableHTML .= "</tbody>\n</table>\n";
echo $tableHTML;
echo "<div class='code'><br />" . $sql1 . "<p>" . $sql2 . "</div>\n";
}
?>
</div>
</body>
</html>
I understand what I would have to change in the second file in order to get the results I want but im having trouble just populating the second drop down in the first file.
Any help would be appreciated. Thanks.
You have missed a double quote in search.php
// Put genders in the options
foreach($stmt->fetchAll() as $genders) {
echo "<option value ='" . $genders['gender'] . "'>" .
$genders['gender'] . "</option>\n";
}
I have a script written in Mysql that just lists people's names. It works fine in mysql but i have been trying to get it to update to mysqli and i got most of it. However, the results are supposed to be paginated. The pagination works fine because there should be 5 pages and thats where it stops....but ALL of the results just show up on every page. Not sure what i am doing...any help would be greatly appreciated....I know it has to do with "mysql_result"
Here is my new code in mysqli:
<?php
include ('paginate.php'); //include of paginat page
$per_page = 10; // number of results to show per page
$result = $con->query("SELECT EmployeeID,FirstName,LastName FROM employee;");
//If Database Error...Print the error and exit
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
//If No Database Error.....Continue
if ($result)
{
// Return the number of rows in result set
$total_results=mysqli_num_rows($result);
//printf("Result set has %d rows.\n",$total_results);
// Free result set
}
//total pages we going to have
$total_pages = ceil($total_results / $per_page);
//if page is setcheck
$show_page='';
if (isset($_GET['page'])) {
$show_page = $_GET['page'];//it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval(isset($_GET['page']));
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View All Employees</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="logo">
<img src="../admin/images/logo-thompson-industrial-services.gif" class="text- center"width="259" height="59" />
</div>
</div>
<br />
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
while($row = mysqli_fetch_assoc($result)) {
$id = $row['EmployeeID'];
$fname = $row['FirstName'];
$lname = $row['LastName'];
echo "<tr>";
echo '<td class="col-md-4">' .$fname .'</td>';
echo '<td class="col-md-4">' .$lname .'</td>';
echo "</tr>";
}
}
// close table>
echo "</table>";
?>
</div>
</div>
</div>
</div>
</body>
</html>
And here is my original code in mysql:
<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
error_reporting(E_ALL ^ E_DEPRECATED);
include('config.php'); //include of db config file
include ('paginate.php'); //include of paginat page
$per_page = 10; // number of results to show per page
$result = mysql_query("SELECT * FROM employee");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have
//-------------if page is setcheck------------------//
$show_page='';
if (isset($_GET['page'])) {
$show_page = $_GET['page']; //it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval(isset($_GET['page']));
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Employees</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<style type="text/css">
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="logo">
<img src="../admin/images/logo-thompson-industrial-services.gif" class="text-center"width="259" height="59" />
</div>
</div>
<br />
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . mysql_result($result, $i, 'FirstName') . '</td>';
echo '<td>' . mysql_result($result, $i, 'LastName') . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
</div>
</div>
</div>
</div>
</body>
</html>
I am doing something wrong here and i cant figure it out....
Any help would be greatly appreciated...
Thanks a lot.
I would also just remove the while statement and use mysqli_data_seek in conjunction with mysqli_fetch_accoc. The MySQLi_data_seek points to the result I need. Check it out:
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First
Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
//go to the column you want the results
mysqli_data_seek($result, $i);
$row = mysqli_fetch_assoc($result);
// echo out the contents of the row into a table
echo "<tr>";
echo '<td>' . $row['FirstName'] . '</td>';
echo '<td>' . $row['LastName'] . '</td>';
echo "</tr>";
}
}
echo "</table>";
The way you are extracting the query is the problem. Once you enter the while statement it does not leave until all the results are done and then it executes
if ($i == $total_results) {
break;
}
So it never gets a chance to end at $i greater than $total_result. The fix is to include LIMIT and OFFSET in the query. That means you have to fix the pagination page too.
So just run the query again like:
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First
Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// run the query again
$sql ="SELECT EmployeeID,FirstName,LastName FROM employee LIMIT $per_page OFFSET $end;
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
$id = $row['EmployeeID'];
$fname = $row['FirstName'];
$lname = $row['LastName'];
echo "<tr>";
echo '<td class="col-md-4">' .$fname .'</td>';
echo '<td class="col-md-4">' .$lname .'</td>';
echo "</tr>";
}
}
echo "</table>";
hello I try to combine the two scripts from the book PHP 6 and MySQL 5 for Dynamic Web Sites. I did search and pagination, but when I go to the next page - did not work.
I posted two screenshots below.
if someone could show me how I make a mistake I will be grateful.
<?php require_once("../../includes/functions_2.php"); ?>
<?php
//database connect
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "1qazxsw2";
$dbname = "dw_bookstore";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//sprawdzenie polaczenia
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//zmaiana znako na utf8
if (!mysqli_set_charset($connection, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($connection));
} else {
//printf("Kodowanie ustawione na: %s\n", mysqli_character_set_name($connection));
}
?>
<?php
// Number of records to show per page:
$display = 3;
// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.
$pages = $_GET['p'];
} else { // Need to determine.
#$query = $_GET['query'];
$query4 = "SELECT COUNT(id) ";
$query4 .= "FROM photographs ";
$query4 .= "WHERE `nazwa` LIKE '%".$query."%' ";
//$query .= "WHERE visible = 1 ";
$result = #mysqli_query ($connection, $query4);
$row = #mysqli_fetch_array ($result, MYSQLI_NUM);
$records = $row[0];
// Count the number of records:
if ($records > $display) { // More than 1 page.
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
} // End of p IF.
// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
// Make the query:
#$query = $_GET['query'];
$query3 = "SELECT * ";
$query3 .= "FROM photographs ";
$query3 .= "WHERE `nazwa` LIKE '%".$query."%' ";
$query3 .= "OR `kod` LIKE '%".$query."%' ";
//$query .= "AND visible = 1 ";
$query3 .= "ORDER BY id ASC LIMIT $start, $display ";
$result3 = mysqli_query ($connection, $query3);
?>
<?php
// 2. Perform database query
$query2 = "SELECT * ";
$query2 .= "FROM photographs ";
//$query2 .= "WHERE visible = 1 ";
$query2 .= "ORDER BY nazwa ASC ";
$result2 = mysqli_query($connection, $query2);
// Test if there was a query error
if (!$result2) {
die("Database query failed.");
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>List_n01</title>
<link href="../../stylesheets/main_2.css" rel="stylesheet" type="text/css" media="screen, projection" />
</head>
<body>
<div id="wrapper">
<div id="header">
<h2>Cennik: Panel Administracyjny</h2>
</div>
<div id="mainContent">
<h1>Graphic Design</h1>
<?php
// Count the number of returned rows:
$num = mysqli_num_rows($result2);
if ($num > 0) { // If it ran OK, display the records.
// Print how many rows there are:
echo "<p>W bazie znajduje się $num pozycji.</p>\n"; ?>
<form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="GET">
<fieldset>
<ul class="formList">
<li>
<input type="text" name="query" placeholder="Szukana fraza... " />
<input type="submit" value="Search" />
</li>
</fieldset>
</form>
<table id="article">
<caption></caption>
<colgroup>
</colgroup>
<tr>
<th>Zdjęcie:</th>
<th>Typ:</th>
<th>Wielkość:</th>
<th>Nazwa:Nazwa:</th>
<th>Kod:</th>
<th>Edytuj:</th>
<th>Szczegóły:</th>
<th>Usuń:</th>
</tr>
<?php
// 3. Use returned data (if any)
while($row = mysqli_fetch_assoc($result3)) {
// output data from each row
?>
<tr>
<td><img src="../images/<?php echo $row['filename']; ?>" width="150" class="article" /></td>
<td><?php echo $row['type']; ?></td>
<td><?php echo size_as_kb($row['size']); ?></td>
<td><?php echo $row['nazwa']; ?></td>
<td><?php echo $row['kod']; ?></td>
<td>Edytuj</td>
<td>Detale</td>
<td>{Usuń}</td>
</tr>
<?php
}
?>
</table>
<?php
// 4. Release returned data
mysqli_free_result($result3);
} else { // If no records were returned.
echo '<p class="error">There are currently no rows.</p>';
}
?>
<?php
// Make the links to other pages, if necessary.
if ($pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button:
if ($current_page != 1) {
echo 'Previous ';
}
// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
$distance = $current_page - $i;
if (abs($distance) < 5){
echo '' . $i . ' ';
}
} else {
echo $i . ' ';
}
} // End of FOR loop
// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo 'Next';
}
echo '</p>'; // Close the paragraph.
} // End of links section.
?>
</div>
<div id="footer">
<p>Copyright <?php echo date("Y", time()); ?>, Cleoni</p></div>
</div>
</body>
</html>
<?php
// 5. Close database connection
mysqli_close($connection);
?>
you are facing issues because in your second url, the query parameter is missing, you should have also have the query=car parameter in get as the data that is been searched is searched with that parameter according to the script...
Change code from around line 184-204 to the following
// If it's not the first page, make a Previous button:
if ($current_page != 1) {
echo 'Previous ';
}
// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
$distance = $current_page - $i;
if (abs($distance) < 5){
echo '' . $i . ' ';
}
} else {
echo $i . ' ';
}
} // End of FOR loop
// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo 'Next';
}
<?PHP
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="one.css" type="text/css">
</head>
<?
include_once "db.php";
$result = mysql_query("SELECT * FROM products WHERE product_type='weddingdressaline' ORDER BY user_id");
{
echo "<table width='100%' border='5' bordercolor='#860071' cellspacing='5' cellpadding='5'>";
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
$i = 0;
while ($i < 3)
{
echo '<td><table width="100%" border="0" cellspacing="0" cellpadding="0" BGCOLOR="#AA99C5"><TR><TD><div class="container"><img src="showimage.php?user_id=' . $row["user_id"] . '" ALIGN="CENTER" /></div></td><tr><td><CENTER><STRONG>' . $row['price'] . '</STRONG></td></TABLE>';
$i++;
}
echo "</tr>";
}
} else {
echo "<tr><td colspan='" . ($i + 1) . "'>No Results found!</td></tr>";
}
echo "</table>";
}
?>
The code is suppose to put one item into one column. Unfortunately its placing one result for the entire row. If anyone has any ideas how to fix this please help.
You have some access brackets in your code:
$result = mysql_query("SELECT * FROM products WHERE product_type='weddingdressaline' ORDER BY user_id");
{ <===
echo "<table width='100%' border='5' bordercolor='#860071' cellspacing='5' cellpadding='5'>";
[....]
echo "</table>";
} <===
First of all please learn some clean coding standards for everyones sake:
http://drupal.org/coding-standards
The second problem is that your are outputting this in the wrong. Trying to use tables for one (use divs) is just poor markup these days. All your styles should be in your CSS, not embedded on the page.
Here is your greatly improved markup. You use CSS to change how it is displayed on the page, not a table structure.
<html>
<head>
<title></title>
<link rel="stylesheet" href="one.css" type="text/css">
</head>
<body>
<?php
include_once "db.php";
$result = mysql_query("SELECT * FROM products WHERE product_type = 'weddingdressaline' ORDER BY user_id");
echo '<div class="productTable">';
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_object($result)) {
echo '
<div class="container">
<div class="productImage">
<a href="viewproduct.php?user_id=' . $row->user_id . '">
<img src="showimage.php?user_id=' . $row->user_id . '" align="center" />
</a>
</div>
<div class="price">' . $row->price . '</div>
</div>';
}
}
else {
echo '<div class="container"><div class="noResults">No Results found!</div></div>';
}
echo "</div>";
?>
</body>
</html>