php Load subjects withe looping throug users object - php

i am a biginner in php and i have started building my project but this problem stops me ..
look at my function
my problem is i use load subjects in order in a page but after load it i need from each user to have 25 subjects if he/she have this ammount of subjects in DB to appear in the page in order so in my case some users have only one subject and the others have more than 25 subject then the user that have one subject if he is shown at the end of the page in first load of the page then the rest of the other subjects will be dessappear untill i press the view more btn
but acually what i need is to have orderd subjects as it is made by one user ? any help ??
:)
public static function getsubjects($users){
$output = array();
foreach ($users as $key => $fo){
$sql ="SELECT * FROM supject_content WHERE userid = '$fo->hostid' ORDER by subjectid DESC LIMIT 25";
$query= mysql_query($sql);
if($query){
if(mysql_num_rows($query)> 0){
while($row = mysql_fetch_object($query)){
$output[]=$row;
}
}
}
}
return $output ;
}

Related

php mysql RAND() in foreach loop

I made page with all profiles. Every 3 rows I inserting a Premium block between results. I have 12 rows on page and 4 premium blocks between every 3 rows. I need to show random users with premium='1' row from mysql every foreach loop.
$sth = $db->prepare("SELECT count(*) as premiumCount FROM users WHERE premium='1'");
$sth->execute();
$answer = $sth->fetch();
$premium = $answer['premiumCount'];
$sth2 = $db->prepare("SELECT * FROM users WHERE active='1' AND block='0' ORDER BY premium DESC LIMIT 12");
$sth2->execute();
$result = $sth2->fetchAll();
foreach ($results as $answer) {
// getting user info
if ($premium > 0) {
$sth = $db->prepare("SELECT * FROM users WHERE premium='1' ORDER BY RAND()");
// getting premium user info
}
}
This code is work, but it can be 2-3 same premium blocks on page (I need 4 different blocks).
Select as many random premium users as you need before the loop with one query (you can add LIMIT 4 to your query) instead of selecting repeatedly inside the loop. That way you can be sure you won't have duplicates.
Fetch the results and store them in an array, and where you're running the "SELECT * FROM users WHERE premium='1'... query inside the loop, use a value from the array instead. You can use array_pop to get values so you won't have to mess with a counter or anything like that to keep track of your position in the array.
// getting premium user info
$sth = $db->prepare("SELECT * FROM users WHERE premium='1' ORDER BY RAND() LIMIT 4");
$sth->execute();
$premium = $sth->fetchAll();
foreach ($results as $answer) {
// getting user info
if ($premium) {
$premium_row = array_pop($premium);
}
}

How to get a field from a result set and print as a string to the page (php/mysql/phpbb)

Im trying to make a custom function for my forum based on php. I have my index landing style page separate to my phpbb forums. I am trying to display the latest registered users username, as part of a statistics type widget for the home page. I have the following code so far:
//Function code
//Total Posts calculation
$result = $mysqli->query("SELECT DISTINCT post_id FROM phpBB_posts
WHERE post_id <= '1'");
$total_posts = $result->num_rows;
//Total topics calculation
$result2 = $mysqli->query("SELECT DISTINCT topic_id FROM phpBB_topics WHERE topic_id <= '1'");
$total_topics = $result2->num_rows;
//Member count calculation
$result3 = $mysqli->query("SELECT DISTINCT user_id FROM phpBB_users WHERE user_id <= '1'");
$total_members = $result3->num_rows;
//Newest member
$result4 = $mysqli->query("SELECT * FROM phpBB_users WHERE group_id <> 6 ORDER BY user_regdate DESC LIMIT 1");
$newestMember = $result4->name;
//End of function
//function output
printf("Total Posts: ".$total_posts."<br/> Total Topics: ".$total_topics."<br/>Total Members: ".$total_members."<br/>Our newest member is: ".$newestMember);
The "Newest member" section is where i'm having trouble, the rest works as I want, if you have any pointers/criticism i'll gladly take it on board though.
I want to be able to return the value in the username column from the results set i get returned from that query, and print it as the variable $newestMember.
My logic as you can see was to get the list of users, so SELECT * then limit the list to actual users and exclude bots etc so WHERE group_id <> 6. Then sort the list by the registered date ORDER BY user_regdate and then only one row from the full list with DESC LIMIT 1. I've been checking mysql and php documentation and can't work out how to get the value from the returned row/username column (field) and store it as the newestMember variable and print to the page.
Can anyone point me in the right direction please?
Cheers, Jamie
Managed to get it working modifying that code a little, deleted the mysqli->query that prepended the SELECT query and that made it work. What finally worked is as per below if anyone else comes across the same issue. Thanks for the nudge in the right direction CBroe! Needed a fresh pair of eyes and to look at it from a different angle :)
//Newest member
$myresult = ("SELECT * FROM phpBB_users WHERE user_type <> 2 ORDER BY user_regdate DESC LIMIT 1");
if ($result = $mysqli->query($myresult)) {
/* fetch object array */
while ($row = $result->fetch_row()) {
$newestMember = $row[7];
}
/* free result set */
$result->close();
}
//End of function
//function output
printf("Total Posts: ".$total_posts."<br/> Total Topics: ".$total_topics."<br/>Total Members: ".$total_members."<br/>Our newest member is: ".$newestMember);

PHP Session Variable lost after click on page other category

I have one site such as OLX.com for used product buy and sell, index page has city filter drop down list, once user clicks filter he will see only product from his filter city and redirect to page city_filter.php till now everything is working,
after city filter when user click on any categories such as computer or Electronics user not found any product, all categories are coming from mysql database, city_filter.php included function_city.php for calling function for all categories, please find city_filter.php function created on function_city.php as follow:
function displaycomputer_city(){
global $con;
$get_cats = "select * from categories where categories_id = 1";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats)) {
$computer_id = $row_cats['categories_id'];
$computer_title = $row_cats['categories_name'];
echo "<li><a href='city_filter.php?computer_cat=$computer_id'>$computer_title</a></li>";
}
}
On city_filter.php page, when I click Computer it take on browser computer_cat=id and I am using mention function to filter computer only from filter City
function getcomputerpro_city() {
global $con;
global $eu;
global $limit;
if(isset($_GET['computer_cat'])){
$cat_id = $_GET ['computer_cat'];
$get_products = "SELECT product_list.*, city1.* FROM product_list INNER JOIN city1 ON product_list.city_id=city1.city_id AND product_list.city_id={$_SESSEION['city_id']} AND categories_id='$cat_id' limit $eu, $limit";
foreach ($con->query($get_products) as $row) {
echo "<div class='ser-grid-list'>
<h5>$row[product_title]</h5>
<img src='admin_area/product_images/$row[image_1]' width='160' height='110' alt=''>
<p>Price : $row[currency] $row[product_price]</p>
<p>City : $row[city_name]</p>
<p>Date : $row[date]</p>
<div class='btn top'><a href='details.php?pro_id=$row[list_id]'>View More</a></div>
</div>";
}
}
}
I think I am not going with correct why, because what I noticed when I click on Computer categories it takes computer_cat = id which is on receive using GET method, and once click categories city id session is lost, I don't what is the practice for this type of situation.
If you have included session_start(); in the start of every page and if you have not unset/destroyed sessions, you have a typo here:
Change this:
$get_products = "SELECT product_list.*, city1.* FROM product_list INNER JOIN city1 ON product_list.city_id=city1.city_id AND product_list.city_id={$_SESSEION['city_id']} AND categories_id='$cat_id' limit $eu, $limit";
to this:
$get_products = "SELECT product_list.*, city1.* FROM product_list INNER JOIN city1 ON product_list.city_id=city1.city_id AND product_list.city_id={$_SESSION['city_id']} AND categories_id='$cat_id' limit $eu, $limit";

Search list of Facebook friends

I am trying to search a user's Facebook friends list against entries in a gamers table in an application database.
The gamers table looks like id, name, points. Eg. g_id-234567, name-john Smith, points-45. In this table a person's id, is actually their Facebook id.
I am using the following code in my Codeigniter application with the Facebook php sdk to return list of friends for the logged in user.
$friends = $facebook->api('/me/friends');
foreach ($friends["data"] as $value) {
echo $value['id'];
}
This code is in my index function of my controller, and it successfully echos the ids of all the friends of the logged in user.
How do I check if any of my Facebook friends id matches that of any gamer g_id in my gamers table.
I want to get the list of gamers who are my Facebook friends, and order this list in descending order of points witha limit of 20. This is to get the top 20 Facebook friends gamers.
I would like to use this list of the logged in user's top 20 Faccebook friends in my view.
Try this (If you are using mysqli)
$id = -1;
$list = array();
$stmt = $mysqli->prepare("SELECT * FROM gamers where id = ? ORDER BY points DESC LIMIT 20");
$stmt->bind_param("i", $id);
foreach($friends["data"] as $value) {
$id = $value['id'];
$stmt->execute();
$res = $stmt->get_result();
array_push($list,$res->fetch_row());
}
I hope, $list should contain the list of all facebook gamers.
Alternatively, you can use :
$friends_set = '(';
foreach($friends["data"] as $value) {
$friends_set .= $value['id'].',';
}
$new_set = preg_replace('/,$/',')',$friends_set);
$res = mysql_query("SELECT * from gamers WHERE id IN $new_set ORDER BY points DESC LIMIT 20");
while($row = mysql_fetch_array($res,MYSQLI_ASSOC) {
//do your stuff
}
Inside last loop you may traverse list of facebook gamers.

Improve processing speed of web page in PHP Mysql

My website take too much to load.I want to show a table on top of page and on bottom of page.
To get the same result twice,I am using same query two times.What is the best way to fetch data only once so that we improve speed?
Here is my code AT TOP
$q_s ="SELECT id,ref,name,description FROM TABLE WHERE status=2 AND is_archive=0
ORDER BY id DESC limit 5";
$res = $objApp->query($q_s);
while($rw = $objApp->row($res)){
//display my data here in div1
}
AT BOTTOM
$q_s2 ="SELECT id,ref,name,description FROM TABLE WHERE status=2 AND is_archive=0
ORDER BY id DESC limit 5";
$res2 = $objApp->query($q_s2);
while($rw = $objApp->row($res2)){
//display my data here in div2
}
NOTE:DIV1 and DIV2 are two different html.Also i showed only simplest query,its too complecated in my case.
How can i improve it?Sorry i am new to PHP
Here is what I would do
$rows = array();
$q_s ="SELECT id,ref,name,description FROM TABLE WHERE status=2 AND is_archive=0
ORDER BY id DESC limit 5";
$res = $objApp->query($q_s);
while($row = $objApp->row($res)){
array_push($row, $rows);
}
You can use $data in a foreach loop for an example
foreach($rows as $row) {
echo $row['colname'];
}
Now you can use data anywhere, multiple times in the page without doing any more queries, hope this helps :)

Categories