?
Solved

IF statement problem

Posted on 2009-02-24
16
Medium Priority
?
199 Views
Last Modified: 2013-12-12
I want to vary whether a link is shown to users based on their userID (which eventually will be got from a session). The problem is that a number of users should be able to see this link (say a maximum of five) however I can only get it to show when the $LecUsername is set to the value of the first row returned in the query. I kind of thought it would do this however can't work how to check it against all rows in the query

What I have at the moment is:

$query2 = mysql_query("SELECT LecUsername
FROM Lecturer, Modules_Lecturers, Survey
WHERE Lecturer.lecturerID = Modules_Lecturers.lecturerID
AND Modules_Lecturers.moduleID = Survey.ModuleID
AND Survey.SurveyID = '5'");

$row2 = mysql_fetch_array($query2);

$LecS = $row2['LecUsername'];
$LecUsername = "ehcxr1";


if ($LecS == $LecUsername) {
echo'<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].' | <a onClick="showHideTag(this,\'Tag'.$qcnt2.'\');return false;">Add New Tag</a></p>';
}
else {
echo '<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].'</p>';
}
0
Comment
Question by:Chocula
  • 9
  • 4
  • 3
16 Comments
 

Author Comment

by:Chocula
ID: 23724553
I should add its not possible to use permissions as I am authenticating users via an imap server. Its complicated but is the reason why.
0
 
LVL 16

Expert Comment

by:Robin
ID: 23724705
How about this:
$query2 = mysql_query("SELECT LecUsername
FROM Lecturer, Modules_Lecturers, Survey
WHERE Lecturer.lecturerID = Modules_Lecturers.lecturerID
AND Modules_Lecturers.moduleID = Survey.ModuleID
AND Survey.SurveyID = '5'");
 
$go=0;
$result=mysql_query($query2);
while($row2=mysql_fetch_array($result)) {
  if($row2['LecUsername']=="ehcxr1") $go=1;
}
 
if ($go == 1) {
 echo'<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].' | <a onClick="showHideTag(this,\'Tag'.$qcnt2.'\');return false;">Add New Tag</a></p>';
}
else {
 echo '<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].'</p>';
}

Open in new window

0
 
LVL 14

Expert Comment

by:Ionut A. Tudor
ID: 23724754
try the below code

<?php
$query2 = mysql_query("SELECT LecUsername
FROM Lecturer, Modules_Lecturers, Survey
WHERE Lecturer.lecturerID = Modules_Lecturers.lecturerID
AND Modules_Lecturers.moduleID = Survey.ModuleID
AND Survey.SurveyID = '5'") or die(mysql_error());
 
while($row2 = mysql_fetch_array($query2)){
 
$LecS = $row2['LecUsername'];
$LecUsername = "ehcxr1";
 
 
if ($LecS == $LecUsername) {
echo'<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].' | <a onClick="showHideTag(this,\'Tag'.$qcnt2.'\');return false;">Add New Tag</a></p>';
}
else {
echo '<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].'</p>';
}
 
}
 
 
?>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Chocula
ID: 23724776
i get this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource. This is all being performed within a while statement already, will that make any difference?
0
 
LVL 16

Expert Comment

by:Robin
ID: 23724831
Yes. Can you post the while loop also?
0
 

Author Comment

by:Chocula
ID: 23724860
the second answer works however it is repeating the:

<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].'</p>

for each row in the table...
0
 

Author Comment

by:Chocula
ID: 23724876
here is the while loop. I'm very sorry about the code, its got very messy last night (up until early hours of the morning)


$query = mysql_query("SELECT FeedbackText, StudentID, FeedbackDate, FeedItemID, QuestionID 
FROM FeedbackItem JOIN FeedbackSurvey
ON FeedbackItem.FeedbackID = FeedbackSurvey.FeedbackID
WHERE FeedbackSurvey.SurveyId = '$surveyID'
ORDER BY QuestionID ASC LIMIT $start, $max_news");
 
 
 
// Check if there are any rows in our table
if (mysql_num_rows($query) > 0) {
$qcnt2=1;
$qcnt=1;
$ifr=1;
// Print out the results (default 10 per pages)
while($row = mysql_fetch_array($query)) {
 
$FeedItem = $row['FeedItemID'];
 
 
$amount = mysql_query("SELECT COUNT(FeedItemID) AS CommCount FROM FeedbackComment WHERE FeedItemID = {$row['FeedItemID']}");
 
$countC = mysql_fetch_array($amount);
 
 
 
echo'<div id="contBG">';
echo'<div id="xsnazzy">';
echo'<b class="xtop"><b class="xb1"></b><b class="xb2"></b><b class="xb3"></b><b class="xb4"></b></b>';
echo'<div class="xboxcontent">';
 
$query2 = mysql_query("SELECT LecUsername
FROM Lecturer, Modules_Lecturers, Survey
WHERE Lecturer.lecturerID = Modules_Lecturers.lecturerID
AND Modules_Lecturers.moduleID = Survey.ModuleID
AND Survey.SurveyID = '5'") or die(mysql_error());
 
while($row2 = mysql_fetch_array($query2)){
 
$LecS = $row2['LecUsername'];
$LecUsername = "jgcxr1";
 
 
if ($LecS == $LecUsername) {
echo'<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].' | <a onClick="showHideTag(this,\'Tag'.$qcnt2.'\');return false;">Add New Tag</a></p>';
}
else {
echo '<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].'</p>';
}
 
}
 
 
echo'<p id="Tag'.$qcnt2.'" style="display:none;"><IFRAME width="450" SRC="addTag.php?id='.$FeedItem.'" frameborder="0" scrolling="no" height="30px"></IFRAME></p>';
echo'<p class"feedText">'.$row['FeedbackText'].'</p>';
echo'<p>'.$countC["CommCount"].' Comments Left | <a onClick="showHideAddComment(this,\'Comments'. $qcnt .'\');return false;">+ Add/View Comment</a></p>';
echo'</div>';
echo'<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>';
echo'</div>';
echo '</div>'; // End ContBG
 
 
 
echo '<div id="Comments'. $qcnt .'" style="display:none;">';
echo '<div class="Comments">';
 
$RowCount = $countC["CommCount"];
 
if ($RowCount == 0) {
$Height = 220;
}
 
if ($RowCount == 1) {
$Height = 330;
}
 
if ($RowCount == 2) {
$Height = $RowCount * 230;
}
 
if ($RowCount == 3) {
$Height = $RowCount * 190;
}
 
if ($RowCount == 4) {
$Height = $RowCount * 170;
}
 
if ($RowCount == 5) {
$Height = $RowCount * 155;
}
 
if ($RowCount == 6) {
$Height = $RowCount * 148;
}
 
if ($RowCount == 7) {
$Height = $RowCount * 141;
}
 
if ($RowCount == 8) {
$Height = $RowCount * 138;
}
 
if ($RowCount == 9) {
$Height = $RowCount * 131;
}
 
if ($RowCount == 10) {
$Height = $RowCount * 126;
}
 
if ($RowCount > 10) {
$Height = $RowCount * 140;
}
 
 
echo '<IFRAME width="500" SRC="CommentsIFrame.php?id='.$FeedItem.'" frameborder="0" scrolling="no" height="'.$Height.'px"></IFRAME>';
echo '</div>';
echo '</div>';
 
 
$qcnt2++;
$qcnt++;
echo '<p class="lineSpace"></p>';
}

Open in new window

0
 

Author Comment

by:Chocula
ID: 23724906
in that example I had changed the code to al3cs12 answer
0
 
LVL 14

Expert Comment

by:Ionut A. Tudor
ID: 23724962
if ($LecS == $LecUsername) {

means you need to have a a field `LecUsername` in your database with `jgcxr1` value.
replace this
$LecS = $row2['LecUsername'];

with this
echo $LecS = "LecUsername value= ".$row2['LecUsername']."<br>";
 
And see if it matches jgcxr1
0
 
LVL 16

Expert Comment

by:Robin
ID: 23725006
In that example you already changed it? It looks fine... ?
0
 

Author Comment

by:Chocula
ID: 23725152
This is getting confusing. In your code? Before making the above change I got this:

------

Feedback left by: mcai6ty6 | 2009-02-22

Feedback left by: mcai6ty6 | 2009-02-22 | Add New Tag

---

so its obviously just looping all the rows (there are only two at the moment)

after changing to use the echo:
-------
LecUsername value= jgcxr1

Feedback left by: mcai6ty6 | 2009-02-22
LecUsername value= ehcxr1

Feedback left by: mcai6ty6 | 2009-02-22
-----------

is what i now get.

what i want is when the username is there, display this once:

Feedback left by: mcai6ty6 | 2009-02-22 | Add New Tag

if not:

Feedback left by: mcai6ty6 | 2009-02-22
0
 

Author Comment

by:Chocula
ID: 23725162
that was directed at al3cs12:
0
 
LVL 14

Accepted Solution

by:
Ionut A. Tudor earned 2000 total points
ID: 23725284
replace with below and it should work

$query = mysql_query("SELECT FeedbackText, StudentID, FeedbackDate, FeedItemID, QuestionID 
FROM FeedbackItem JOIN FeedbackSurvey
ON FeedbackItem.FeedbackID = FeedbackSurvey.FeedbackID
WHERE FeedbackSurvey.SurveyId = '$surveyID'
ORDER BY QuestionID ASC LIMIT $start, $max_news");
 
 
 
// Check if there are any rows in our table
if (mysql_num_rows($query) > 0) {
$qcnt2=1;
$qcnt=1;
$ifr=1;
// Print out the results (default 10 per pages)
while($row = mysql_fetch_array($query)) {
 
$FeedItem = $row['FeedItemID'];
 
 
$amount = mysql_query("SELECT COUNT(FeedItemID) AS CommCount FROM FeedbackComment WHERE FeedItemID = {$row['FeedItemID']}");
 
$countC = mysql_fetch_array($amount);
 
 
 
echo'<div id="contBG">';
echo'<div id="xsnazzy">';
echo'<b class="xtop"><b class="xb1"></b><b class="xb2"></b><b class="xb3"></b><b class="xb4"></b></b>';
echo'<div class="xboxcontent">';
 
$query2 = mysql_query("SELECT LecUsername
FROM Lecturer, Modules_Lecturers, Survey
WHERE Lecturer.lecturerID = Modules_Lecturers.lecturerID
AND Modules_Lecturers.moduleID = Survey.ModuleID
AND Survey.SurveyID = '5'") or die(mysql_error());
 
while($row2 = mysql_fetch_array($query2)){
 
$LecS = $row2['LecUsername'];
$LecUsername = "jgcxr1";
 
 
if ($LecS == $LecUsername) {
$feed_info = '<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].' | <a onClick="showHideTag(this,\'Tag'.$qcnt2.'\');return false;">Add New Tag</a></p>';
break;
}
else {
$feed_info = '<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].'</p>';
}
 
}
 
 echo $feed_info;
 
echo'<p id="Tag'.$qcnt2.'" style="display:none;"><IFRAME width="450" SRC="addTag.php?id='.$FeedItem.'" frameborder="0" scrolling="no" height="30px"></IFRAME></p>';
echo'<p class"feedText">'.$row['FeedbackText'].'</p>';
echo'<p>'.$countC["CommCount"].' Comments Left | <a onClick="showHideAddComment(this,\'Comments'. $qcnt .'\');return false;">+ Add/View Comment</a></p>';
echo'</div>';
echo'<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>';
echo'</div>';
echo '</div>'; // End ContBG
 
 
 
echo '<div id="Comments'. $qcnt .'" style="display:none;">';
echo '<div class="Comments">';
 
$RowCount = $countC["CommCount"];
 
if ($RowCount == 0) {
$Height = 220;
}
 
if ($RowCount == 1) {
$Height = 330;
}
 
if ($RowCount == 2) {
$Height = $RowCount * 230;
}
 
if ($RowCount == 3) {
$Height = $RowCount * 190;
}
 
if ($RowCount == 4) {
$Height = $RowCount * 170;
}
 
if ($RowCount == 5) {
$Height = $RowCount * 155;
}
 
if ($RowCount == 6) {
$Height = $RowCount * 148;
}
 
if ($RowCount == 7) {
$Height = $RowCount * 141;
}
 
if ($RowCount == 8) {
$Height = $RowCount * 138;
}
 
if ($RowCount == 9) {
$Height = $RowCount * 131;
}
 
if ($RowCount == 10) {
$Height = $RowCount * 126;
}
 
if ($RowCount > 10) {
$Height = $RowCount * 140;
}
 
 
echo '<IFRAME width="500" SRC="CommentsIFrame.php?id='.$FeedItem.'" frameborder="0" scrolling="no" height="'.$Height.'px"></IFRAME>';
echo '</div>';
echo '</div>';
 
 
$qcnt2++;
$qcnt++;
echo '<p class="lineSpace"></p>';
}

Open in new window

0
 

Author Comment

by:Chocula
ID: 23725384
thank you, ended up just replacing this:

if ($LecS == $LecUsername) {
$feed_info = '<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].' | <a onClick="showHideTag(this,\'Tag'.$qcnt2.'\');return false;">Add New Tag</a></p>';
break;
}
else {
$feed_info = '<p class="UserDetails">Feedback left by: '.$row['StudentID'].' | '.$row['FeedbackDate'].'</p>';
}
 
}
 
 echo $feed_info;


as got white space if i replaced all of it. Seems to be working perfect now so thank you both.
0
 
LVL 14

Expert Comment

by:Ionut A. Tudor
ID: 23725411
The details you specified at #23725152 really helped understand what you wanted, Thanks for the points
0
 

Author Closing Comment

by:Chocula
ID: 31550713
Great help thank you. I am slowly getting my explanations better!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question