8 Replies - 605 Views - Last Post: 11 September 2013 - 10:27 AM Rate Topic: -----

#1 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 31-October 12

yet another unassigned variable

Posted 11 September 2013 - 04:23 AM

I do have error reporting on and I did learn from before and have print_r on:) I'm revising my show_comment(); function. ANd testing it. I do have the variable declared in variables.php file. It's actually the primary key in my database so the number will autoincrement. There is now 1 record in the database for testing purposes so technically it is set. (well, in my mind anyways:) PHP does not think so though:)

<?php



function showcomments($conn) {
If ($id > 0)   {
$show = $conn->prepare("SELECT username, subject, email, website, comment, date FROM comments WHERE page = ? ORDER BY time");
$show->execute(array($_SERVER['REQUEST_URL'])); 
$numRows = $show->rowCount(); 
}
else 
{
	echo "There are no comments to display.  Be the first!";
}
}

?>




If the primary key is greater than 0, show all the comments. If it's 0 (meaning no comments), then display "there are no comments" message. Right now, since it does not know what $id is, it just gives me undefined index and displays that there are o comments text.

Here is where I have the variable declared in variables.php

<?php

$id = $_POST['id'];


?>



I have tried using instead of $id, use plain id (since the others don't take marks around the fieldnames in the mysql statement), and then the error tells me it assumes I meant 'id'. Yes, I did so I put in 'id' and then it also gives me undefined index.
How can I get this id to point to what the primary key is in the database and read it as 0 or greater as intended?
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: yet another unassigned variable

#2 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 31-October 12

Re: yet another unassigned variable

Posted 11 September 2013 - 04:29 AM

I was thinking about all the tihings I tried as I was typing. ANd thinking about the logic and what I was trying to do. (Remember yesterday? haha) I tried everything except putting id as a fieldname in the select statement and then 'id' in the if statement. No error this time.

Awesome.

Except one problem.

Now it only shows the text message there is no comments.

It does not show my test comment that I inserted directly on server for it to show up in the select statement:(
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,143
  • Joined: 08-June 10

Re: yet another unassigned variable

Posted 11 September 2013 - 04:40 AM

from a recent read:

http://open.blogs.nytimes.com/2013/08/27/the-young-developers-guide-to-debugging-javascript/ said:

The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.


back to topic. of course $id is not defined. PHP’s scope does not extend beyond the function’s boundary. hence you need to set $id inside.

or even better, don’t use it at all. use the original.
function showcomments($conn) {
  if ((int) $_POST['id'] > 0) {
    // ...


on the other hand side, you could throw an exception (makes for cleaner code)
function showcomments($conn) 
{
  $id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT, array('options' => array('min_range' => 1)));
  if (!is_int($id)) 
  {
    throw new InvalidArgumentException("ID is invalid or not greater than zero.")
  }
  $conn-> // ...






PS. you indentation s***s

Quote

Now it only shows the text message there is no comments.

why should it show the comments?
Was This Post Helpful? 0
  • +
  • -

#4 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 31-October 12

Re: yet another unassigned variable

Posted 11 September 2013 - 05:04 AM

ok I'll read about these exceptions on my commute this morning and get back to this later.
Meanwhile, why does my indentation suck?:( Aside from what's inside {} what else needs indented?

Why should it show comments? Well, that's what my intention was when I created this

$show = $conn->prepare("SELECT username, subject, email, website, comment, date FROM comments WHERE page = ? ORDER BY date");
$show->execute(array($_SERVER['REQUEST_URL']));
$numRows = $show->rowCount(); 



First prepare (with the databsse connection ($conn). Then execute (and it will get whatever page the user is on), and then show.
1. Not sure if my indention sucks here or not.
2. Yes, the computer is always right. I know I did something wrong so I submit:)
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,143
  • Joined: 08-June 10

Re: yet another unassigned variable

Posted 11 September 2013 - 05:09 AM

Quote

Meanwhile, why does my indentation suck?

because there is (almost) no indentation at all.

Quote

Aside from what's inside {} what else needs indented?

choose any indentation style you like (and you can work with best).

Quote

Why should it show comments? Well, that's what my intention was when I created this

PHP doesn’t care about your intention. it only cares about what you write.

Quote

First prepare (with the databsse connection ($conn). Then execute (and it will get whatever page the user is on), and then show.

and which of the 3 lines is responsible for showing the content?

This post has been edited by Dormilich: 11 September 2013 - 05:11 AM

Was This Post Helpful? 0
  • +
  • -

#6 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 31-October 12

Re: yet another unassigned variable

Posted 11 September 2013 - 05:50 AM

rowcount() but at this point maybe I should check on that:-) [
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,143
  • Joined: 08-June 10

Re: yet another unassigned variable

Posted 11 September 2013 - 05:52 AM

yepp, if in doubt, ask the documentation.
Was This Post Helpful? 0
  • +
  • -

#8 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: yet another unassigned variable

Posted 11 September 2013 - 10:07 AM

View PostDormilich, on 11 September 2013 - 06:52 AM, said:

yepp, if in doubt, ask the documentation.


I agree, This is the best advise you can ever get. Documentation is like the very first place you should turn to.
Was This Post Helpful? 0
  • +
  • -

#9 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 31-October 12

Re: yet another unassigned variable

Posted 11 September 2013 - 10:27 AM

I do agree. You guys happen to see me at my worst :-) I did solve some things thanks to php.net though, one this morning, a timezone issue. Didnt solve it dűe to server restrictions but found a workaround (new php function) and with error detection turned off it wont show up anyways. Its mostly when I have code written that I'm thinking "I thought things were right but I'll ask". An error you can always google. For some reason, when nothing happens, I feel more blind.
Anyways now I got 2 interviews tomorrow so I wont get to this til after. Because I'll be preparing:-) (No its not for PHP lol)

This post has been edited by Dormilich: 11 September 2013 - 11:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1