Continue numbering PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I work on a page(.php) that show some information from databases(like article name,article author).I also have a pagination script that work good(the ideea of pagination script is when I press a link the url of the site goes to something like ?page=2)Now I try to numbering the records that I show form databases.But how can I do that?
Pagination script:
$per_pages = 6;
$nrofarticles_query = mysql_query("SELECT COUNT('id') FROM tbl_articles");
$nrdearticole = mysql_result($nrofarticles_query, 0);
$totalpages = ceil($nrdearticole/$per_pages);
$currentpagedisplay = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$firstpair = ($currentpagedisplay - 1) * $per_pages;
I echo record from database like so:
$articles_set = mysql_query("SELECT * FROM tbl_articles LIMIT $firstpair, $per_pages");
while($article = mysql_fetch_array(article_set)){
echo $article['article_name'];
}
So I show 6 records from database on page change show the next 6 records or if is less than 6 show how many remained .How can I numbering the records like this:
?page=1
1 article_name1
2 article_name2
3 article_name3
....
6 article_name6
?page=2
keep going
7 article_name7
8 article_name8
.....
EDIT
I was thinking to something like
<?php
$nrofarticles = 0;
$articles_set = mysql_query("SELECT * FROM tbl_articles LIMIT $firstpair,$per_pages");
confirm($articles_set);
while($article = mysql_fetch_array($articles_set)){
$nrofarticles ++;
}
echo "<ul id=\"id\">";
for($i = $firstpair+1; $i<=$nrofarticles * $currentpagedisplay; $i++){
echo '<li class="nr_id">' .$i. '<sub>#</sub></li>';
}
echo "</ul>";
?>
It's works but only on first 2 page of 3 because I have 16 articles in total - so on last page $firstpair + 1 will be 13 and $nrofarticles * $currentpagedisplay will be 12(4(articles) * 3(page=3)).

Go use a framework. You'll get pagination (and all sorts of extras) for free.

Related

Get a length of a php's result [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know php at all so sorry for a simple question - I've got the project started by another person so now I'm trying to finish it.
The problem is - I'm making an android app for which I can't make a change without changing a php and this language I don't know.
here's this part :
function getPlaces(){
$result = array();
$sql = "SELECT id, name, short_description, photo_list, selected, recommended, isTOP FROM place
WHERE id IN(SELECT id_place FROM rubric_place WHERE id_rubric IN(SELECT id FROM rubric WHERE name = '".$_REQUEST["rubric"]."')) ORDER BY isTOP DESC";
getConnect();
$query = mysql_query($sql);
if(!$query){
error100();
}else {
$result['code'] = 200;
for ($i = 0; $i < mysql_num_rows($query); $i++) {
$row = mysql_fetch_assoc($query);
$result["places"][$i] = $row;
$sql = "SELECT * FROM rubric WHERE id IN(SELECT id_rubric FROM rubric_place WHERE id_place = ".$row[id].")";
$queryModule = mysql_query($sql);
if($queryModule){
for ($k = 0; $k < mysql_num_rows($queryModule); $k++) {
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rubrics"][$k] = $rowModule;
}
}
$sql = "SELECT SUM(rating)/COUNT(rating) AS rating FROM comment WHERE id_place = ".$row[id];
$queryModule = mysql_query($sql);
if($queryModule){
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rating"] = $rowModule[rating];
}
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit();
}
}
what I need is to make another function that returns in $result a value of "length" of places. I know I can find out length on the other end - in Android's app result, but this particular function will be changed so it will return by 20 results only so I need another function that returns length so plz help
To count the number of characters in a json you can do:
$string = json_encode($result, JSON_UNESCAPED_UNICODE);
$length = strlen($string);
You have to first put the json into a variable to count it, before outputting it.
To count the items in an array before you turn it into a json, you can do:
$length = count($array);

While to check something in the database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to check if I have an hour in the database but If I have more than hour in the database it outputs twice, if I have 3 three times...
In this specific case this gives me: 9 (in blue color) and then 9 (in red color)
$result = mysqli_query($con, 'SELECT * FROM consulta
WHERE professional=1
AND client=0');
while ($row = mysqli_fetch_array($result)) {
if ($row['hora']=='09:00:00') { echo '<p class="blue">9</p>'; }
else { echo '<p class="red">9</p>'; }
}
I have a MySql database with columns: id, professional and hour. I have stored 2 hours and this is what I get if I echo $row['hora'] 09:00:00 and 10:00:00
In this case the if should give me just 9 in blue because it is in the database. I do not understand why it happens. How can I solve this?
This is the structure of the MySql database:
Presumably you are building up a calendar or the like.
The query you are using is returning all rows (and you are looping over each row, regardless of the hour). If you want to check if specific hours exist you could build up an array of hours:
$result = mysqli_query($con, 'SELECT * FROM consulta
WHERE professional=1
AND client=0');
$hours = array();
while ($row = mysqli_fetch_array($result)) {
$hours[$row['hora']] = $row;
}
Now to check a specific hour we can do:
if (!empty($hours['09:00:00'])) {
echo '<p class="blue">9</p>';
}else {
echo '<p class="red">9</p>';
}
Edit: Explanation
In the first loop we are building up an array. This will look like:
$hours = array(
'08:00:00' => $dataForThisRow,
'09:00:00' => $dataForThisRow
);
In the above '08:00:00' is called a key. Each key is a time that was found in the database.
To check if there is an entry in the database for our time we can check if there is a non empty key. Hence we use:
if(!empty($hours['09:00:00'])
This if will run if $hours has a key with the value '09:00:00'. If there is no key then the other part of the if will run.
You are comparing with this value: 09:00:00, but the database seems to have stored this other: 9:00:00
Change your sql query like below:
$query = "SELECT TIME_FORMAT(hora,'%H:%i:%S') FROM consulta
WHERE professional=1
AND client=0";
$result = mysqli_query($con, $query);

mysql php if else inside while loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
$sql = mysql_query("SELECT DISTINCT business_region FROM business WHERE business_category='occupation'");
// count the output amount
$businessCount = mysql_num_rows($sql);
if ($businessCount > 0) {
while ($row = mysql_fetch_array($sql)) {
$business_region = $row["business_region"];
$Region_view .= "display value";
}
}
This gives a following result:
Michigan Toronto Ohio Manchester London Sydney Paris
I want to select only Ohio and London
as a variable value as:
$Region_view .
Perhaps I can use if else structure, but I could not get it to work.
i.e. I need to select only Ohio, London
I can do it using joint MySQL statement but it is too slow so I need and ask for a different way.
$sql = mysql_query("SELECT DISTINCT business_region FROM business WHERE business_category ='occupation'");
// count the output amount
$businessCount = mysql_num_rows($sql);
if ($businessCount > 0) {
while($row = mysql_fetch_array($sql)){
$business_region = $row["business_region"];
if ($business_region == 'Ohio' || $business_region == 'London') // added
$Region_view .= $business_region;//changed
}
}

Validation for 3 instances [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to put a validation for when you get the same date, with the same time and same venue, it will not insert in the database. It will only insert in the database if 1 instance will return false.
$res_date = $_POST['res_date'];
$res_venue = $_POST['res_venue'];
$res_dur = $_POST['res_duration'];
$qryjay = mysql_query("SELECT * FROM tbl_reservation WHERE res_date='$res_date' AND res_dur = '$res_dur' AND res_venue = '$res_venue'");
if ((mysql_num_rows($qryjay) == $res_date) && ($qryjay == $res_dur) && ($qryjay) == $res_venue){
echo "<script language=javascript>alert('".$res_venue." is not available on ".$res_dur." on ".$res_date."!')</script>";
}
Try to fix your code like this:
<?
$res_date = mysql_real_escape_string($_POST['res_date']);
$res_venue = mysql_real_escape_string$_POST['res_venue']);
$res_dur = mysql_real_escape_string$_POST['res_duration']);
$qryjay = mysql_query("SELECT count(*) as countNbr FROM tbl_reservation WHERE res_date='".$res_date."' AND res_dur = '".$res_dur."' AND res_venue = '".$res_venue."'");
$result = mysql_fetch_assoc($qryjay);
if ($result['countNbr'] > 0)){
echo "<script language=javascript>alert('".$res_venue." is not available on ".$res_dur." on ".$res_date."!')</script>";
}
?>

getting 2 item instead of 1 mysql database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have problem i am using below script for merge tables and get data from mysql
$myboxexi=mysql_query('select box.id,box.page_name,box.title,box.connect,box.type,box.uid,box.description,box.image,box.url,box.status,box.date,box.time
from boxes as box left join page_boxes as pages on box.uid=pages.uid and pages.uid="'.$session_id.'"');
while($my_boxi=mysql_fetch_array($myboxexi)){
$title=$my_boxi['title'];
$bid=$my_boxi['id'];
$ptitle=$my_boxi['page_name'];
$connect=$my_boxi['connect'];
$type=$my_boxi['type'];
$iuid=$my_boxi['uid'];
$description=$my_boxi['description'];
$image=$my_boxi['image'];
$url=$my_boxi['url'];
$appr=$my_boxi['status'];
$date=$my_boxi['date'];
$time=$my_boxi['time'];
$myinfo=mysql_query('select * from users where id="'.$iuid.'"');
$my_boxi_info=mysql_fetch_array($myinfo);
$user_id=$my_boxi_info['id'];
$user_name=$my_boxi_info['user_name'];
$thmb=$my_boxi_info['thumb'];
if(strpos($thmb,'https://') !== false) {
$thmbs=$thmb;
}else
{
$thmbs='images/thumbs/'.$thmb;
}
}
its working fine but problem is i am getting double item like if i have 1 item then i get 2 item how can i solve this issue ?
I think you get 2 values because you have for example,element [0] and same element with key=name of column , so use only associatif result :
Change this :
while($my_boxi=mysql_fetch_array($myboxexi)){
//data here.
}
To this :
while($my_boxi=mysql_fetch_array($myboxexi, MYSQL_ASSOC)){
//data here.
}
Or to this
while($my_boxi=mysql_fetch_assoc($myboxexi)){
//data here.
}
mysql_fetch_array() returns a dual-keyed array. e.g.
SELECT foo FROM bar
$row = mysql_fetch_array($result);
will give you the array:
$row = array(
0 => 'value from foo',
'foo' => 'value from foo'
);
Since you're blindly looping on the returned array, you're looking up each of those "duplicate" results. If you only want ONE of the field types, try the dedicated functions, or the optional type argument,e .g
$row = mysql_fetch_assoc($result); // return only string keys
$row = mysql_fetch_array($result, MYSQL_ASSOC); // ditto
Presumably you are getting two items because two pages have the same user/session id that is being passed in:
select box.id, box.page_name, box.title, box.connect,box.type, box.uid, box.description,
box.image, box.url, box.status,b ox.date,box.time
from boxes box left join
page_boxes pages
on box.uid = pages.uid and pages.uid = "'.$session_id.'"'
(To me, it is suspicious that you are setting uid -- which often means "user id" -- to a variable called $session_id, but that is another matter.)
If you only want one, add limit 1 to the end of the query:
select box.id, box.page_name, box.title, box.connect,box.type, box.uid, box.description,
box.image, box.url, box.status,b ox.date,box.time
from boxes box left join
page_boxes pages
on box.uid = pages.uid and pages.uid = "'.$session_id.'"'
limit 1;

Categories