list of friend not being display using php and mysql - php

i have a list of friend that i want to show on each profile page where the user is friend with them but the problem is that when i use firebug the system display one id and with a default piture
so i think the error is in the foreach loop can anyone help me ????
php code:
<?php
//***********************Displaying Friend List*************************//
$friendListTitle = "";
$friendList = "";
if($friend_array!="")
{
$friendArray = explode(",", $friend_array);
$friendArray = array_slice($friendArray,0,6);
$friendCount = count($friendArray);
var_dump($friendCount);
$friendListTitle = '<div class="title"> '.$username.'\'s Friends('.$friendCount.')</div>';
//iterating to retrieve what it's needed as values
/*$frnd1 = $friendArray[0];
$frnd2 = $friendArray[1];
/*$frnd3 = $friendArray[2];
$frnd4 = $friendArray[3];
$friendList .='<div style="background-color:"#CCC";>'.$frnd1.'<br />'.$frnd2.'</div>';*/
$i=0;
$friendList .='<div style="background-color:"#CCC"; >';
foreach($friendArray as $key => $value)
{
$i++;
$check_pic = "members/$value/image01.jpg";
if(file_exists($check_pic))
{
$frnd_pic = '<img src = \"$check_pic\" width = \"30px\"; border = \"1\"/>';
}
else
{
$frnd_pic = '<img src = "members/0/image01.jpg" width = \"30px\" border = \"1\"/> ';
}
$sqlName = mysql_query("SELECT first_name, last_name FROM members WHERE user_id= '$value' LIMIT 1") or die(mysql_error());
while($row = mysql_fetch_array($sqlName))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList = '<div title="'.$fname.' '.$lname.'">'.$frnd_pic.'</div>';
}
}
$friendList .='</div>';
}
?>

Your html table structure is broken:
add closing bracket here <table border = "0" align="center" cellpadding="3", add td tags, put tr tags into loop
also, replace
if($row = mysql_fetch_array($sqlName,MYSQL_ASSOC))
with
while($row = mysql_fetch_array($sqlName,MYSQL_ASSOC))

You have missed one dot. Please look at this
while($row = mysql_fetch_array($sqlName))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList .= '<div title="'.$fname.' '.$lname.'">'.$frnd_pic.'</div>';
}
because the varibale's name ($friendList) you need to make some string concatenation not simple substitution.
If You use substitution then everythinh what you have collected in $friendList will be lost.
The main problem was in this line:
$friendList .= '<div title="'.$fname.' '.$lname.'">'.$frnd_pic.'</div>';

Related

PDO - Fetch Assoc in a 'while' loop

I'm trying to echo a bit of html within a while loop. I'm getting my stuff with PDO and PDO_ASSOC.
This is what I have:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book_id = $row['id'];
$book_title = $row['title'];
$book_image = $row['image'];
$book_amz = $row['amazon'];
$book_desc = $row['description'];
$book_rating = $row['rating'];
$book_date = $row['date'];
$book_author = $row['author'];
$book_categorie = $row['categorie'];
$text = "ID: ' . $book_id . '";
}
return $text;
But it gives me only one row of the table. I even tried fetchAll, but it gives me nothing.
So making the assumption that the only element ever seen is the last element it is because what your are returning is being overwritten each loop. There are a few options to resolve this. The simplest is:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$text = "";
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book_id = $row['id'];
$book_title = $row['title'];
$book_image = $row['image'];
$book_amz = $row['amazon'];
$book_desc = $row['description'];
$book_rating = $row['rating'];
$book_date = $row['date'];
$book_author = $row['author'];
$book_categorie = $row['categorie'];
//String concatenation of text will
//give you one big string at the end to return.
$text .= "ID: '{$book_id}'";
}
return $text;
However this will not work well with your real bootstrap html. You need to make sure that the columns add up right.
You will need something a bit more intuitive
Using the actual code it would look something like
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$bookEcho = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$bookEcho[] = '<div class="col-md-3">
<div class="thumbnail">
<span>' . $book_title . '</span>
<img src="' . $book_image . '">
<div class="book-options">
<span>Bewertung</span><br/>
' . $stars . '
Jetzt lesen
</div>
</div>
</div>';
}
return $bookEcho;
Now in your function what ever it is you can do something like (this is not the most elegant thing I have ever written but should get the job done):
$cols = 4;
$colCount = 1;
foreach ($bookEcho as $book){
if($colCount == 0){//create a row}
echo $book;
$coolCount++;
if($colCount == 0){end a row}
if($colCount == 4){ $colCount = 0;}
}
The problem is that you're overwriting your values in your while loop. The loop is being executed once for each entry in the database, but only the last one will be returned. Instead you want to use arrays:
$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$books = [];
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book['id'] = $row['id'];
$book['title'] = $row['title'];
$book['image'] = $row['image'];
$book['amz'] = $row['amazon'];
$book['desc'] = $row['description'];
$book['rating'] = $row['rating'];
$book['date'] = $row['date'];
$book['author'] = $row['author'];
$book['categorie'] = $row['categorie'];
$book['text'] = "ID: ' . {$book['id']} . '"; // << Not sure if this is what you actually want. If not, adjust accordingly.
// Append the above values to the $books array
$books[] = $book;
}
return $books;
Use a foreach:
$books = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($books as $book){
$book_id = $book['id'];
$book_title = $book['title'];
// ...
}

display list of friend user is not showing all users

I have a function that retrieve list of friends from the MySQL table using the friend_array field.
But the problem is that the browser doesn't show more than one picture, but it shows the default one.
this is the code
<?php
//***********************Displaying Friend List*************************//
$friendList = "";
$friendListTitle="";
if($friend_array!="")
{
$friendArray = explode(",", $friend_array);
$friendArray = array_slice($friendArray,0,6);
$friendCount = count($friendArray);
$friendListTitle = '<div class="title"> '.$username.'\'s Friends('.$friendCount.')</div>';
//iterating to retrieve what it's needed as values
/*$frnd1 = $friendArray[0];
$frnd2 = $friendArray[1];
/*$frnd3 = $friendArray[2];
$frnd4 = $friendArray[3];
$friendList .='<div style="background-color:"#CCC";>'.$frnd1.'<br />'.$frnd2.'</div>';*/
$i=0;
$friendList ='<div style="background-color:"#CCC"; >';
foreach($friendArray as $key => $value)
{
$i++;
$check_pic = "members/$value/image01.jpg";
if(file_exists($check_pic))
{
$frnd_pic = '<img src = \"$check_pic\" width = "52px" border = "1"/>';
}
else
{
$frnd_pic = '<img src = "members/0/image01.jpg" width = "52px" border = "1"/> ';
}
$sqlName = mysql_query("SELECT first_name, last_name FROM members WHERE user_id= '$value'LIMIT 1") or die(mysql_error());
if($row = mysql_fetch_array($sqlName,MYSQL_ASSOC))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList = '<div title="'.$fname.' '.$lname.'">'.$frnd_pic.'</div>';
}
}
$friendList.='</div>';
}
?>
you images and divs that contain the records of friends is being replaced by next record so it display the last record of your friends.
try this may help you
$friendList = "";
$friendListTitle="";
if($friend_array!="")
{
$friendArray = explode(",", $friend_array);
$friendArray = array_slice($friendArray,0,6);
$friendCount = count($friendArray);
var_dump($friendCount);
$friendListTitle = '<div class="title"> '.$username.'\'s Friends('.$friendCount.')</div>';
//iterating to retrieve what it's needed as values
$i=0;
$friendList ='<div style="background-color:"#CCC"; >';
foreach($friendArray as $frndlist => $value)
{
$i++;
$check_pic = 'members/'.$value.'/image01.jpg';
if(file_exists($check_pic))
{
//storing each friend images separately
$frnd_pic[$i] = '<img src = "'.$check_pic.'" width = "52px" border = "1"/>';
}
else
{
$frnd_pic[$i] = '<img src = "members/0/image01.jpg" width = "52px" border = "1"/> ';
}
$sqlName = mysql_query("SELECT first_name, last_name FROM members WHERE user_id= '$value'LIMIT 1") or die(mysql_error());
if ($row = mysql_fetch_array($sqlName, MYSQL_ASSOC))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList .= '<div title="'.$fname.' '.$lname.'">'.$frnd_pic[$i].'</div>';
}
}
$friendList.='</div>';
}

how to use foreach inside while loop?

Currently I am using following code to get data sorted by starting letter of name, if you run this code you will get what i am trying to create
<?php
$dirs = array('Aname1','Aname2','Aname3','A Nmae','Bname ','Cname','Cardiff','Dname','Dname',);
$cur_let = null;
foreach ($dirs as $dir) {
if ($cur_let !== strtoupper(substr($dir,0,1))){
$cur_let = strtoupper(substr($dir,0,1));
echo "<li class=\"title\">".$cur_let."</li>";
}
echo "<li class=\"clear\">
<div class=\"name\">".$dir."</div>
<div class=\"mobile\"></div>
<div class=\"telephone\"></div>
<div class=\"email\"></div>
<div class=\"action\">edit | delete</div>
<div class=\"clear\"></div>
</li>";
}
but how to use above loop inside following to get vales from database and it should be display like (I want highlight first letter) http://i.stack.imgur.com/bLHVD.jpg
$query = "SELECT * FROM phone_number";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$names = $row['name'].",";
}
?>
With MySQLi:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = $mysqli->query($query);
while($row = $sql->fetch_assoc()) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
With mysql_* deprecated functions:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = mysql_query($query);
while($row = mysql_fetch_array($sql)) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
Try something like this:
$query = "SELECT * FROM phone_number ORDER BY name DESC";
$result = mysql_query($query) or die(mysql_error());
$lastLetter = '';
$html = '<ul>';
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
if (strtoupper($name[0]) !== $lastLetter) {
if ($lastLetter !== '')
$html .= '</ul></li>';
$lastLetter = strtoupper($name[0]);
$html .= '<li class="title">' . $lastLetter;
$html .= '<ul>';
}
$html .= '<li>' . $name . '</li>';
}
$html .= '</ul></li></ul>';

Passing variables without a form using url

I have been trying to find information on passing variables without a form using url, but I can't seem to find anythig that pertains to my situation.
Basically I need to have a page that is hierachical in the sense that you can click one course out of a list, that brings you to a list of sections, and you can click on a section which will bring to a page with the correct information for that one section. I was taught for this situation to use $_SESSION and then to include the variables in the url. My problem is that everytime I click on a course or a section, the information for every single course or section that is a query match is displaying rather than just the information for that one course or section. This is what I have for each of the pages in hierarchical order (not including the connect portion of the code which works fine anyways). I know this is a ton of code, and a complete mess. I will keep searching out information, and many many thanks in advance for any help.
List of Courses
session_start();
$section = $_SESSION['section_id'];
$user_fname = $_SESSION['user_fname'];
$user_lname = $_SESSION['user_lname'];
$query = "SELECT DISTINCT course_t.course_id, course_name, user_fname, user_lname, section_t.section_id
FROM course_t, authorized_user_t, section_t, teaching_history_t
WHERE authorized_user_t.authorized_user_id = teaching_history_t.instructor_id
AND teaching_history_t.section_id = section_t.section_id
AND section_t.course_id = course_t.course_id";
$result = #mysql_query($query);
while ($line1 = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<p align = "center"><strong>' .$line1['course_id'].':'.$line1['course_name']. '</strong><br><br>';
}
List of Sections (it is supposed to list a single intructor and then each of the sections they teach under their name)
session_start();
$section_id = $_REQUEST['section_id'];
$user_fname = $_REQUEST['user_fname'];
$user_lname = $_REQUEST['user_lname'];
$course_id = $_SESSION['course_id'];
$course_name = $_SESSION['course_name'];
$section_id = $_SESSION['section_id'];
$semester = $_SESSION['semester'];
$year = $_SESSION['year'];
$course_description = $_SESSION['course_description'];
$SGoal_Description = $_SESSION['SGoal_Description'];
$sobjective_description = $_SESSION['sobjective_description'];
$LObjective_Description = $_SESSION['LObjective_Description'];
$topic_title = $_SESSION['topic_title'];
$topic_description = $_SESSION['topic_description'];
$coursework_title = $_SESSION['coursework_title'];
$coursework_location = $_SESSION['coursework_location'];
$coursework_description = $_SESSION['coursework_description'];
$query = "SELECT course_t.course_id, course_name, section_t.section_id, semester, year, course_description,
SGoal_Description, sobjective_description, LObjective_Description, topic_title, topic_description, coursework_title,
coursework_location, coursework_description, syllabus
FROM course_t, section_t, section_goal_section_t, section_goal_t, section_coursework_t,
mapped_topic_section_t, mapped_lis_section_t, coursework_t, topic_t, lis_objective_t, section_objective_t
WHERE course_t.course_id = section_t.course_id
AND section_t.section_id = section_goal_section_t.section_id
AND section_goal_section_t.sgoal_id = section_goal_t.sgoal_id
AND section_goal_section_t.section_id = mapped_lis_section_t.section_id
AND mapped_lis_section_t.lobjective_id = lis_objective_t.lobjective_id
AND section_t.section_id = mapped_topic_section_t.section_id
AND mapped_topic_section_t.topic_id = topic_t.topic_id
AND section_t.section_id = section_coursework_t.section_id
AND mapped_lis_section_t.sobjective_id = section_objective_t.sobjective_id
AND section_coursework_t.coursework_id = coursework_t.coursework_id";
$result = #mysql_query($query);
while ($line1 = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<ul align = "center"<strong>' .'Instructor'. ':' ."$user_fname". "$user_lname".'<li align = "center">
<a href ="course_section.php?course_id='.$line1['course_id'].'&course_name='.$line1['course_name'].'&section_id='.$line1['section_id'].'
&semester='.$line1['semester'].'&year='.$line1['year'].'&course_description='.$line1['course_description'].'
&SGoal_Description='.$line1['SGoal_Description'].'&sobjective_description='.$line1['sobjective_description'].'
&LObjective_Description='.$line1['LObjective_Description'].'&topic_title='.$line1['topic_title'].'
&topic_description='.$line1['topic_description'].'&coursework_title='.$line1['coursework_title'].'
&coursework_location='.$line1['coursework_location'].'&coursework_description='.$line1['coursework_description'].'">'.$line1['section_id'].'</a></strong><br><br>';
}
Section Information (should display information for the section that is clicked on)
session_start();
$topic_description = $SESSION['topic_description'];
$coursework_description = $SESSION['coursework_description'];
$course_id = $_REQUEST['course_id'];
$course_name = $_REQUEST['course_name'];
$section_id = $_REQUEST['section_id'];
$semester = $_REQUEST['semester'];
$year = $_REQUEST['year'];
$course_description = $_REQUEST['course_description'];
$SGoal_Description = $_REQUEST['SGoal_Description'];
$sobjective_description = $_REQUEST['sobjective_description'];
$LObjective_Description = $_REQUEST['LObjective_Description'];
$topic_title = $_REQUEST['topic_title'];
$topic_description = $_REQUEST['topic_description'];
$coursework_title = $_REQUEST['coursework_title'];
$coursework_location = $_REQUEST['coursework_location'];
$coursework_description = $_REQUEST['coursework_description'];
echo '<div align = "right">';
echo 'Return to List of Sections';
echo '</div>';
$query = "SELECT DISTINCT course_t.course_id, course_name, section_t.section_id, semester, year, course_description,
SGoal_Description, sobjective_description, LObjective_Description, topic_title, topic_description, coursework_title,
coursework_location, coursework_description, syllabus
FROM course_t, section_t, section_goal_section_t, section_goal_t, section_coursework_t,
mapped_topic_section_t, mapped_lis_section_t, coursework_t, topic_t, lis_objective_t, section_objective_t
WHERE course_t.course_id = section_t.course_id
AND section_t.section_id = section_goal_section_t.section_id
AND section_goal_section_t.sgoal_id = section_goal_t.sgoal_id
AND section_goal_section_t.section_id = mapped_lis_section_t.section_id
AND mapped_lis_section_t.lobjective_id = lis_objective_t.lobjective_id
AND section_t.section_id = mapped_topic_section_t.section_id
AND mapped_topic_section_t.topic_id = topic_t.topic_id
AND section_t.section_id = section_coursework_t.section_id
AND mapped_lis_section_t.sobjective_id = section_objective_t.sobjective_id
AND section_coursework_t.coursework_id = coursework_t.coursework_id";
$result = #mysql_query($query);
while ($line1 = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<p align = "center"><strong>' .$line1['course_id'].':'.' '.$line1['course_name']. '</strong><br><br>';
echo '<p align = "center"><strong>' .$line1['section_id'].':'.' '.$line1['semester'].'/'.$line1['year']. '</strong><br><br>';
echo '<p align = "left"><strong>' .$line1['course_description'].'</strong><br><br><br><br>';
echo '<div align = "left">';
echo '<ul align = "left"><strong>Section Goals:</strong></ul><li align = "left">' .$line1['SGoal_Description']. '</li>';
echo '<ul align = "left"><strong>Section Objectives:</strong></ul><li align = "left">' .$line1['sobjective_description']. '</li>';
echo '<ul align = "left"><strong>Mapped Objectives:</strong></ul><li align = "left">' .$line1['LObjective_Description']. '</li>';
echo '<ul align = "left"><strong>'.'Topic'.':'.' '.$line1['topic_title']. '</strong></ul>
<li align = "left">Click for Topic Description';
echo '<ul align = "left"><strong>'.'Coursework'.':'.' '.$line1['coursework_title']. '</strong></ul>
<li align = "left">Click for Coursework Description';
}
Don't use sessions, just include it in the url ex: example.com/index.php?example=true. If you use $_GET['example'] it will have the value 'true'.

PHP Functions with multiple MySQL results only returning one record

This is the function that im calling.
function GetSubmissions($coach){
$result = mysql_query("SELECT * FROM `ptable` WHERE coach = '$_SESSION[username]'") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$id = $row['id'];
$teampre = $row['team'];
$eventpre = $row['event'];
$statuspre = $row['status'];
$eventarray = DecodeEvent($eventpre);
$event = $eventarray[0];
$cat = $eventarray[1];
$subcat = $eventarray[2];
$division = $eventarray[3];
$type = $eventarray[4];
$teamarray = explode(",", $teampre);
foreach ($teamarray AS $tkey => $tvalue){
$result = mysql_query("SELECT * FROM `students` WHERE id = '$tvalue'") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $skey => $svalue) { $row[$skey] = stripslashes($svalue); }
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$teamgo .= "$firstname $lastname<br/>";
}
}
$push .= "<div id=submission><div id=event>$event</div><div id=status>$statuspre</div><div id=subinfo>$cat $subcat $division $type</div><div id=team>$teamgo</div></div>";
}
return $push;
}
It works, except its only returning a single result. Ive made little tweaks here and there, but im not seeing any positive changes in the output. Any ideas where im going wrong?
You have nested $result variables. You should try to avoid using the same variable name twice. Renaming the second one inside your second foreach loop would probably do the trick.
You use the same $result for inner loop.

Categories