/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] IF Statement not working correctly…

Hello,

One more problem(I promise this is the last one!)

I want it to display ‘Comment edited by Admin etc’ if there is a ‘Y’ in the admin field of the table. Unfortunately, the following displays it even if there is an ‘N’ in the field :

[code=php]if ($row4[2] = ‘Y’) {
echo ‘<small> – Comment edited by Admin on ‘ . $row4[‘udate’] . ‘</small></p>’;
}
else {
echo ‘<br /></p>’;
}
}[/code]

Obviously if there is an ‘N’ in there, I don’t want to display anything ?

For comments that have an [B]‘N’[/B] in the ‘admin’ field it displays :

[CODE]– Comment edited by Admin on 0000-00-00[/CODE]

For comments that have an [B]‘Y’[/B] in the ‘admin’ field it displays :

[CODE]– Comment edited by Admin on 2006-04-01[/CODE]

Any clues ?

The problem code is down at the bottom of this script.

[code=php]
<?

require_once (‘./includes/config.inc.php’);

$page_title = ‘Picture’;
include (‘./includes/header.html’);

function par($str) {
return eregi_replace(“rnrn”, “</p>n<p>”, $str);
}

// Page Header
echo ‘<h2>Picture</h2>’;

require_once (‘../mysql_connect.php’); // Connect to the database

if (isset($_POST[‘submitted’])) { // Handle the Comments form.

// Check for Comments, use strip_tags
$com = strip_tags(stripslashes($_POST[‘comment’]));

if (empty($com)) {
echo ‘<h3>Please fill in the comments field</h3>’;
include (‘./includes/footer.html’);
exit;
}

if ($com) {

if(!$_SESSION || !$_SESSION[‘first_name’]) {
echo ‘<h3>For this area you will need to login.</h3>’;
echo ‘<b>Options :</b>’;
echo ‘<ul><li><a href=”javascript:history.go(-1)”>Go back to the picture</a></li>’;
echo ‘<li><a href=”login.php”>Login</a></li>’;
echo ‘<li><a href=”register.php”>Register</a></li>’;
echo ‘<li><a href=”forgot_password.php”>Forgot Password</a></li>’;
include (‘./includes/footer.html’);
exit;
}

$query = “SELECT user_id FROM users WHERE first_name = ‘$_SESSION[first_name]'”;
$result = mysql_query($query) or trigger_error(“Query: $queryn<br />MySQL Error: ” . mysql_error());
$row = mysql_fetch_array($result);

$query5 = “SELECT id FROM user_images WHERE id = ‘imgid'”;
$result5 = mysql_query($query5) or trigger_error(“Query: $query5n<br />MySQL Error: ” . mysql_error());
$row5 = mysql_fetch_array($result5);

$query6 = “SELECT id, comment, uid, imgid FROM comments WHERE uid = ” . $_SESSION[‘user_id’] . ” AND imgid = ” . $_POST[‘id’] . “”;
$result6 = mysql_query($query6) or trigger_error(“Query: $query6n<br />MySQL Error: ” . mysql_error());
$row6 = mysql_fetch_array($result6);
$commented = $row6[‘comment’];

} else { // If it didn’t work correctly.
echo ‘<p><font color=”red” size=”+1″>Please type your comment into the Comments field, and try again.</font></p>’;
}
if (mysql_num_rows($result6) < 1) {

$query2 = “INSERT INTO comments(comment,uid,imgid) VALUES(‘$com’, $row[user_id], $_POST[id])”;
$result2 = mysql_query($query2) or trigger_error(“Query: $query2n<br />MySQL Error: ” . mysql_error());
// Finish the page
echo ‘<H3>Thank you for adding a comment for the picture.</h3>’;
include (‘./includes/footer.html’); // Include the HTML footer.
exit();
}
else {
echo ‘<font color=”red”>You have already commented </font>’ . $commented . ‘<font color=”red”> for the picture!</font>’;
}
}

$query = “SELECT img_src, content_type, image_name, area, cameratype, dateUpload, comments, approved, lon, lat, width, height, aid, user_id FROM user_images WHERE id = $_GET[id] AND approved=’Y'”;
$result = mysql_query($query) or trigger_error(“Query: $queryn<br />MySQL Error: ” . mysql_error());
$row = mysql_fetch_array($result);

if (mysql_num_rows($result) == 1) {
$query3 = “SELECT user_id, username FROM users WHERE user_id = $row[user_id]”;
$result3 = mysql_query($query3) or trigger_error(“Query: $query3n<br />MySQL Error: ” . mysql_error());
$row3 = mysql_fetch_array($result3);

echo ‘<p><b>Title : </b>’ . $row[‘image_name’] . ‘<b> , Area : </b>’ . $row[‘area’] . ‘</p>’;
echo ‘<p><b>Posted by : </b><a href=”view_profile.php?id=’ . $row3[‘user_id’] . ‘”>’ . $row3[‘username’] . ‘</a> on ‘ . $row[‘dateUpload’] . ‘</p>’;
echo ‘<p><b>Latitude : </b>’ . round($row[‘lat’], 2) . ‘N &nbsp;&nbsp;<b>Longitude : </b>’ . round($row[‘lon’], 2) . ‘W&nbsp;&nbsp;</p>’;
echo ‘<p><b>Comments : </b><br>’ . par($row[‘comments’]) . ‘</p>’;

if ($row[‘width’] < 1 && $row[‘height’] < 1) {
echo ‘<img src=”show_picture.php?id=’. $_GET[‘id’] .'” alt=”‘ . $row[‘image_name’] . ‘”><p></p>’;
} else {
echo ‘<a href=”show_picture.php?id=’. $_GET[‘id’] .'” target=”_blank”><img src=”show_picture.php?id=’. $_GET[‘id’] .'” alt=”‘ . $row[‘image_name’] . ‘” width=”‘ . $row[‘width’] . ‘” height=”‘ . $row[‘height’] . ‘” border=”1″></a><p></p>’;
}
echo ‘<i><a href=”problempic.php?id=’ . $_GET[‘id’] . ‘”>Notify Admin about “‘ . $row[‘image_name’] . ‘” Picture ?</a></i><br><br>’;

echo ‘<b>Rate a picture today!</b>’;
echo ‘<form name=”rate” action=”vote.php” method=”POST”>’;
echo ‘0<input type=”radio” name=”rating” value=”0″ onClick=”this.form.submit();”>&nbsp;&nbsp;1<input type=”radio” name=”rating” value=”1″ onClick=”this.form.submit();”>&nbsp;&nbsp;2<input type=”radio” name=”rating” value=”2″ onClick=”this.form.submit();”>&nbsp;&nbsp;3<input type=”radio” name=”rating” value=”3″ onClick=”this.form.submit();”>&nbsp;&nbsp;4<input type=”radio” name=”rating” value=”4″ onClick=”this.form.submit();”>&nbsp;&nbsp;5<input type=”radio” name=”rating” value=”5″ onClick=”this.form.submit();”>’;
echo ‘<input type=”hidden” name=”submitted” value=”TRUE” />’;
echo ‘<input type=”hidden” name=”image_name” value=”‘.$row[‘image_name’].'” />’;
?>
<input type=”hidden” name=”id” value=”<?=$_GET[‘id’];?>”>
<?

$query = “SELECT AVG(vote) AS avgvote FROM vote WHERE imgid = $_GET[id]”;
$result = mysql_query($query) or trigger_error(“Query: $queryn<br />MySQL Error: ” . mysql_error());
$row = mysql_fetch_array($result);

echo ‘</form>Current Average Vote: ‘. round($row[‘avgvote’], 2) .'<hr>’;

echo ‘<p><u><b>Comments from other users :</b></u><br />’;
$row = mysql_fetch_array($result);
$query4 = “SELECT c.comment, c.udate, c.admin, u.user_id, u.username FROM comments as c, users as u WHERE imgid = $_GET[id] AND uid = user_id”;
$result4 = mysql_query($query4) or trigger_error(“Query: $query4n<br />MySQL Error: ” . mysql_error());
while ($row4 = mysql_fetch_array($result4)) {
echo ‘<p>’ . $row4[‘comment’] . ‘ – <a href=”view_profile.php?id=’ . $row4[‘user_id’] . ‘”>’ . $row4[‘username’] . ‘</a><br />’;

if ($row4[2] = ‘Y’) {
echo ‘<small> – Comment edited by Admin on ‘ . $row4[‘udate’] . ‘</small></p>’;
}
else {
echo ‘<br /></p>’;
}
}

echo ‘<p><u>Add your own comment on this image</u></p>’;
echo ‘<form action=”picture.php?id=’.$_GET[‘id’].'” method=”POST”><textarea NAME=”comment” COLS=40 ROWS=6></TEXTAREA><br /><br />’;
echo ‘<INPUT TYPE=SUBMIT VALUE=”Add your Comment” name=”submit”><br /><br />’;
echo ‘<input type=”hidden” name=”submitted” value=”TRUE” />’;
?>
<input type=”hidden” name=”id” value=”<?=$_GET[‘id’];?>”>
<?
echo ‘</form>’;
}
else {
echo ‘Image has yet to be approved’;
}
include (‘./includes/footer.html’);
?>
[/code]

Many thanks once more for taking a look.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@the_treeApr 02.2006 — if ($row4[2] [COLOR=Blue]=[/COLOR] 'Y')should beif ($row4[2] [COLOR=Blue]==[/COLOR] 'Y')

It's worth remembering the difference.[code=php]if($foo = 'bar'){
// can foo be set to bar?
}

if($foo == 'bar'){
// is foo equal to bar?
}[/code]
Copy linkTweet thisAlerts:
@invisionauthorApr 02.2006 — It's learning ASP that's done it to me! I swear!

Thanks

?
Copy linkTweet thisAlerts:
@girApr 02.2006 — it's worth remembering the difference.

PHP Code:
[code=php]
if($foo = 'bar'){
// can foo be set to bar?
}
[/code]
[/quote]


are you sure about that? isn't that assign 'bar' to $foo and test the truth of $foo?
Copy linkTweet thisAlerts:
@the_treeApr 02.2006 — It might be, I don't think it's something I've ever used.
Copy linkTweet thisAlerts:
@girApr 02.2006 — you've never used this? :-
[code=php]
if($row = mysql_fetch_assoc($result))
[/code]
Copy linkTweet thisAlerts:
@the_treeApr 02.2006 — nope, I'm only a hobbyist so I don't build error-handling into anything I do, it's always so simple that I'm pretty much certain that it'll work.
Copy linkTweet thisAlerts:
@girApr 03.2006 — i see
Copy linkTweet thisAlerts:
@chazzyApr 03.2006 — to be honest, the only code i've seen using [code=php]if($row = mysql_fetch_assoc($result))[/code] has been code where the person posts on here "How come I'm missing a row in my result/i'm getting no data"
Copy linkTweet thisAlerts:
@girApr 03.2006 — I've written it enough times to know that I'm right. You can be both consider yourself educated.

[code=php]

$foo = true;
$bar = false;

if($bar = $foo) {
echo 'bar is now ' . var_export($bar,true);
}
echo '<ol>';
$arr = array(1,3,5,7,9); // expected five rows of data
while($row = array_pop($arr)) {
echo '<li>'.$row . '</li>';
}
echo '</ol>';

[/code]


Outputs:
<i>
</i>bar is now true&lt;ol&gt;&lt;li&gt;9&lt;/li&gt;&lt;li&gt;7&lt;/li&gt;&lt;li&gt;5&lt;/li&gt;&lt;li&gt;3&lt;/li&gt;&lt;li&gt;1&lt;/li&gt;&lt;/ol&gt;
×

Success!

Help @invision spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...