creating a top list in PHP - php

So my brain is having trouble functionning this morning and I can't figure out how to create a Top list.
I have my query like so
$moyennepisode = $conn->prepare('SELECT * FROM show_moyenne, shows WHERE shows.id = show_moyenne.show_id ORDER BY moyenne DESC LIMIT 100');
$moyennepisode->execute();
while ($show = $moyennepisode->fetch()) {
somecode }
Which is all fine but I simply want to output the position so I tried a for loop but I can't seem to figure out where to place it. I tried an associate array but I failed at that, too.
Can anyone recommend how to do that ? And if there might be a way to do have the position from mmysql instead of php ?
Any help appreciated. Thanks!

$position = 1;
while ($show = $moyennepisode->fetch()) {
Output $position;
$position++;
somecode;
}

Related

Multiple loops in flat database

http://projects.ourplanet.tk/junetxtdb/
http://code.google.com/p/junetxtdb/ in cms (LekkiCMS)
Now my question is how change this code :
$query = $db->select('wizyty');
foreach($query as $record) {
$idz = $record['pro'];
$query2 = $db->select('wizyty_zabiegi',array('id'=>$idz));
foreach($query2 as $record2) {
$record2['name'];
}
}
Every table has lots of values
table wizyty has organized this way
ID"PID"pro"date"time"value1"value2
1"1"2"2020-12-30"12:00"1"2
2"1"1"2020-05-30"12:00"1"2
and table wizyty_zabiegi this way
ID"name"TIMES"HOW
1"Masaż stóp"25"100
2"Masaż nóg"25"100
i use even for (valuepro = valueid)
but this show only good in first result
third, five , six etc result dont show correctly
thank you ;)
i know in mysql i have join but in this project i dont have it ;(
i wrote this question and i found solution hahah
$is= $record['zabieg'] -1;
$query2 = $db->select('wizyty_zabiegi');
$result .= $query2[$is]['nazwa'];
thanks for everything even for reading this messages

How to set a php variable from a sql database?

Im trying to set a php variable to a field and row of a database. I use:
$moped_select = "SELECT * FROM moped_details WHERE moped_ID='$_SESSION[moped_number]'";
$selected_moped = $db->query($moped_select);
if (($selected_moped->rowCount()) >= 1) {
foreach($selected_moped as $row){
$moped_rate = $row['rate'];
}
This doesn't set $moped_rate as the field and row, I have seen other people use this line before and it works. Whats even weirder is that I use a similar line to this to place data in a form. Which looked like this (keep in mind it is inside a foreach(x as $row) loop:
echo "<td>".$row['model']."</td>";
This echos out correctly so im kinda confused why Any suggestions would be appreciated <3.
Your code should as below
$db=new PDO('mysql:host=localhost','username','password');
$moped_select = "SELECT * FROM moped_details WHERE moped_ID=?";
$selected_moped = $db->prepare($moped_select);
$db->execute([$_SESSION["moped_numberf"]]);
if(($db>rowCount()) >= 1) {
while($row=$selectedmoped->fetch(PDO::FETCH_ASSOC){
$moped_rate = $row['rate'];
}

PHP/MYSQL if else

I am creating a baseball website and I am learning php as I go. I have hit a snag with something I am trying to do; here is what my current code shows:
here is my code:
When I load my files that update the scores, if the game hasn't started the scores are NULL. Is there a code to change it to 0 without having to change it in my database. Any info will be helpful
In your select statement you can use the IFNULL(expr1, 0) function, but you will need to list your columns explicitly instead of using select *. So it would be roughly:
select
game_date,
game_time,
...,
IFNULL(AWAY_SCORE, 0),
IFNULL(HOME_SCORE, 0)
FROM scoreboard;
Yup, by way of PHP. It could also be done in the database query
while($row = $result->fetch_assoc(){
$ascore = $row['score'];
$hscore= $row['score'];
if($ascore == ""){
$ascore = 0;
if($hscore== ""){
$hscore = 0;
}
REST OF YOUR CODE HERE
}
}

How to properly count a query in pdo

I am trying to dynamically load more posts into my system
This is my code...
$stmt = "
SELECT * FROM `acmPosting`
WHERE (`sender`='$thisID' AND `postType`='a')
OR (`recip`='$thisID' AND `sender`='$userID' AND `postType`='a')
OR (`sender` IN ($friendsArray) AND `recip`='$thisID' AND `postType`='a')
ORDER BY `timeSent` DESC LIMIT $startlimit,10";
if($stmtCount = $conn->query($stmt)){
if($stmtCount->fetchColumn() > 0){
$result = acmPosts($conn, $site, $userID, $stmt);
$jsonArray['a'] = $result;
$jsonArray['b'] = 'go';
}else{
$jsonArray['a'] = '<div class="thisOutput" style="padding:12px;">There are no more posts</div>';
$jsonArray['b'] = 'stop';
}
}
Everything works fine until it gets to the last set of posts AKA if the LIMIT is 100,10 but there are 105 posts in the call it won't call any to the fetchColumn().
I hope this question makes sense. Thanks in advance for any help you can give.
EDIT
How can I determine when I have reached the LIMIT and act accordingly
Your description is not very precise but maybe this is what you are looking for:
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

PHP link only passing part of a mysql result

OK i have a php link which is made up of several variables
<a href=\year.php? manufacturer='.$manufacturer.'&fuel_type='.$fuel_type.'&model_type='.$model_type.'>'.$model_type.'</a>
The whole code is really long as it has a lot of pagination, so i will just include the basic query and the loop part.
$query1 = "SELECT Distinct model_type from $tableName where manufacturer='$manufacturer' AND fuel='$fuel_type' LIMIT $start, $limit";
$result = mysql_query($query1);
And then the bottom part where i get and show the results.
$count = 0;
$max = 2;
while($row = mysql_fetch_array($result))
{
$model_type = $row['model_type'];
$count++;
echo '<td class="manu"><div align="center">'.'<a href=\year.php? manufacturer='.$manufacturer.'&fuel_type='.$fuel_type.'&model_type='.$model_type.'>'.$model_type.'</a>'.'</div></td>';
if($count >= $max){
//reset counter
$count = 0;
//end and restart
echo '</tr><tr>';
}
}
now this works fine except when i take the mode type variable from the database it shows as 1 series, however when it is passed in this link it only gets the 1 and doesn't pick up the series.
Thanks
try to pass it like:
'.urlencode($model_type).'
Try To access it on next page with urldecode($_REQUEST['$model_type'])
I am not shure but you probably have a missing encoding Problem.
try this:
'.$model_type.'
Its url_encoding the values so your url doesnt get broken by ',",spaces and so on.
additional Hint:
Enclose your URL with " or ' so you do not get problems at the end.

Categories