Jump to content

Not reaching bottom


oz11

Recommended Posts

Hey. Been a while since I last posted here.

Problem:  the first 8 lines of code work for the top box, but not the bottom box. 

Code in question is this:

<?php 
// bottom
if ($anon_comment== "1"){
?>
 <span style="color: black">Post as anonymous?</span> <input type="checkbox" id="anon" name="anon" value="set" checked>
<?php
} elseif($anon_comment == "2" ) {
	?>
 <span style="color: black">Post as anonymous?</span> <input type="checkbox" id="anon" name="anon" value="set">
	<?php
}
?>

And for illustration purposes here is the second box...

<?php 
// bottom
if ($anon_comment== "1"){
?>
 <span style="color: black">Post as anonymous?</span> <input type="checkbox" id="anon" name="anon" value="set" checked>
<?php
} elseif($anon_comment == "2" ) {
	?>
 <span style="color: black">Post as anonymous?</span> <input type="checkbox" id="anon" name="anon" value="set">
	<?php
}
?>

Like I said it works on line 220 but not line 151. Both are very similar if not the same, but the bottom one never works at all. Its like the conditional statement above is not working the other box (aka "bottom"). The first box works fine and as expected.

Whole code:




<?php
$stmt = $pdo->prepare("SELECT cooment_anon FROM `users` WHERE user_id = ?");
$stmt->execute([$_SESSION['the_usr_id']]); 
$is_anon_set = $stmt->fetch();

if($is_anon_set['cooment_anon'] == 1){
	echo $anon_comment =  "1";
} else {
	echo $anon_comment =  "0";
}

if (isset($_GET['anon'])){
	echo "posting as anon";
}

// ---~ login check ~---

// --- ~~ ---
?>

<style>

.generalclass {
  color: #ffffff;
  text-align: left;
  margin: 10px;
  padding: 10px;
  display: none;
}

</style>
<?php //require_once "../config.php";
// create/display nested comments

// init
$user_id = $_SESSION['the_usr_id'];


	
// fake some data
//$_GET['news_id'] = $news['news_id'];
$_GET['news_id'] = 0; //                                         <-------------------- TESTING
$_COMMENTS_ID= $news['news_id'];

$post = []; // array to hold a trimmed working copy of the form data
$errors = []; // array to hold user/valdiation errors

// post
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	// trim all the data at once
	$post = array_map('trim',$_POST); // if any input is an array, use a recursive trim call-back function here instead of php's trim
	
	// validate inputs here...
	
	// if no errors, use the form data
	if ($post['anon'] == "set"){
		$is_anon = 1;
	} else {
		$is_anon = 0;
	}
	if(empty($errors))
	{
		$sql = "INSERT INTO `comment` (`comment_id`, `parent`, `news_id`, `user_id`, `content`, `karma`, `removed`, `reference____`, 
        `date`, `last_update`, `aproved`, `anonymous`, `image_upld`) VALUES (NULL, ?, ?, ?, ?, '0', '0', '', NOW(), NULL, '1', ?, NULL)";
		$stmt = $pdo->prepare($sql);
		$stmt->execute([ $post['parent_id'], $_COMMENTS_ID, $user_id, $post['comment'], $is_anon ]);
	}
	
	// if no errors, success
	if(empty($errors))
	{
		die(header("Refresh:0"));
	}
}

// get all the rows of data for the requested news id
$sql = "SELECT comment.parent, comment.comment_id, comment.news_id, comment.user_id, comment.content, comment.karma, comment.removed, 
comment.date, comment.last_update, comment.aproved, comment.anonymous, comment.image_upld, users.name, users.avatar FROM `comment` LEFT JOIN `users` 
ON `comment`.`user_id` = `users`.`user_id` WHERE news_id = ? ORDER BY comment_id DESC LIMIT 800";
$stmt = $pdo->prepare($sql);
$stmt->execute([ $_COMMENTS_ID ]);
$comment_data = $stmt->fetchAll(PDO::FETCH_GROUP);

// recursive function to output parent/child data


function list_comments($parent_id, $data, $level=0)
{


	// this just supplies a visual part to the output so you can see what the code does
	//$indent = str_repeat("<span style='color: transparent;'>---- </span>", $level);
	$indent = str_repeat("<span style='color: transparent;'>---- </span>", $level);

	
    //echo "<pre>";
    //print_r($data);
    //echo "</pre>";
	// loop over data for the current parent_id
	foreach($data[$parent_id] as $arr)
	{

		// output the comment and any other information
		if ($arr['anonymous'] == 1){
			$pre_hash = $arr['name']."3434292";
					echo $indent ."<a href='profile.php?anonymous'><img src='site-data/avatars/sir.png' style='width: 30px; float: left; margin: 15px; border-radius: 30px;'></a>";
						echo "<a href='profile.php?anonymous' title='This user is in anonymous mode so cannot be seen'>Anonymous [hash ID: usranon".substr(md5($pre_hash), 0, -10)."</a><br>";

				}else{
							echo $indent ."<a href='profile.php?name={$arr['name']}'><img src='site-data/avatars/{$arr['avatar']}' style='width: 30px; float: left; margin: 15px; border-radius: 30px;'></a>";
								echo "<a href='profile.php?name={$arr['name']}'>{$arr['name']}</a><br>";

				}



		echo "<b><p style='font-family: Arial, Helvetica, sans-serif;'>$indent{$arr['content']}</p></b>";
		// determine and output any child count
		$count = isset($data[$arr['comment_id']]) ? count($data[$arr['comment_id']]) : 0;
		$pl = $count == 0 || $count > 1 ? 'ies' : 'y';
		echo "<br>$indent$count Repl$pl<br>";
		// allow a comment for the current parent
		// you would probably want to use a javascript 'show' operation for this
		?>
        <p>
        <?php echo $indent ?><button class="button" onclick="myFunction('button<?=$arr['comment_id']?>')">Reply</button>
        <div id="button<?=$arr['comment_id']?>" class="generalclass"  style="display:none;">
		<form method="post">
		<input type='hidden' name='parent_id' value='<?=$arr['comment_id']?>'>
		<?=$indent?><label>Comment:<br> 
		<?=$indent?><textarea name="comment" rows="4" cols="50" placeholder="remember to be polite!"></textarea></label>
		<input type="submit"> <a href="includes/emojis.php"
   onclick="window.open(this.href,'targetWindow',
                                   `toolbar=no,
                                    location=no,
                                    status=no,
                                    menubar=no,
                                    scrollbars=yes,
                                    resizable=yes,
                                    width=300px;,
                                    height=500px`);
 return false; style='float: right; padding-left: 30px;'">Show emojissszz</a>
<?php

?>
<?php 
// bottom
if ($anon_comment== "1"){
?>
 <span style="color: black">Post as anonymous?</span> <input type="checkbox" id="anon" name="anon" value="set" checked>
<?php
} elseif($anon_comment == "2" ) {
	?>
 <span style="color: black">Post as anonymous?</span> <input type="checkbox" id="anon" name="anon" value="set">
	<?php
}
?>
		</form>
		
 <br>
        </div>
		<?php
		// recurse if there are children of the current parent
		if(isset($data[$arr['comment_id']]))
		{
			list_comments($arr['comment_id'], $data, $level+1);
		}
	}
} 

// html
?>

<?php
// display any errors
if(!empty($errors))
{
	echo implode('<br>',$errors);
}
?>

<?php
if( $_SESSION["loggedin"] != true){

	echo "This category/board comment system is set to private. You must register/login and join to take part. Click <u><a href='login.php'>here</a></u> to login to your account or to register.</div>";
	include 'footer.php';
	exit();
}


// allow a comment on the main article
?>


<form method="post">
<input type='hidden' name='parent_id' value='0'>
<label>Comment:<br>
<textarea name="comment" rows="4" cols="50" placeholder="remember to be polite!" style="border-color: grey; width: 97%;"><?=$post['content']??''?></textarea></label><br>



<input type="submit"><br>

 <a href="includes/emojis.php"
   onclick="window.open(this.href,'targetWindow',
                                   `toolbar=no,
                                    location=no,
                                    status=no,
                                    menubar=no,
                                    scrollbars=yes,
                                    resizable=yes,
                                    width=300px;,
                                    height=500px`);
 return false; style='float: right; padding-left: 50px;'">Show emojis</a><br>
<?php 
// top
if ($anon_comment== "1"){
?>
 <span style="color: black">Post as anonymous?</span> <input type="checkbox" id="anon" name="anon" value="set" checked>
<?php
} elseif($anon_comment == "0") {
	?>
 <span style="color: black">Post as anonymous?</span> <input type="checkbox" id="anon" name="anon" value="set">
	<?php
}
?>
</form><br>

<div style="overflow-y: scroll; height:400px; border: 1px dashed grey; padding: 30px; margin-right: 30px; border-radius: 10px; ">



<?php
if( $_SESSION["loggedin"] == true){
if($count == 0){
    echo "No comments";
}
}
?>


<?php
// list comments, starting with parent 0
list_comments(0, $comment_data, 0);

?>
</div>

<script>
function myFunction(divid) {

  var x = document.getElementById(divid);  
  
  if (x.style.display == "none") 
  {
    x.style.display = "block";
  } 
  else {
    x.style.display = "none";
  }  
}
</script><br><br>

It should be an anonymous checkbox to allow users to choose if they want to submit anonymously based on a flag stored in the DB - "cooment_anon" which can be true or false (0/1). But the decloration isnt working using the code in he first example, despite it being the same. Strange it should work on one but not the other.

 

Tried pasting [this bellow] in other possible locations as a hack, but never worked :S

$stmt = $pdo->prepare("SELECT cooment_anon FROM `users` WHERE user_id = ?");
$stmt->execute([$_SESSION['the_usr_id']]); 
$is_anon_set = $stmt->fetch();

PS: By "box" i mean the initial comment box and the reply comment boxes. 

Edited by oz11
Link to comment
Share on other sites

Update: oops was using ...

} elseif($anon_comment == "2" ) {

..instead of == 0. Tho still does not work on the second box (from line 151). Still a problem. Not showing anything in fact, using the conditional.

Edited by oz11
Link to comment
Share on other sites

Remo

10 hours ago, dodgeitorelse3 said:

this doesn't look right does it? to declare the variable you don't echo it.

Removing the echo statements still doest work. In honesty it should still assign the values even if I did echo them as well.

Link to comment
Share on other sites

16 minutes ago, ginerjm said:

Why are you stuck with it?  I'm assuming that that 'typo' could be your problem .

Nope. It's just a querk of the DB. The table name is actually known as "cooment_anon" correctly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.