Welcome Guest ( Log In | Register )

3 Pages V < 1 2 3 >  
Reply to this topicStart new topic
> Blog script
Alis Dee
post Nov 20 2006, 03:04 AM
Post #21


Rank #4: Advanced
****

Group: Members
Posts: 127
Joined: 22-November 05
Member No.: 3,379



Close, but you can't stack statements inside a function call like that. Try putting the print statements on their own new lines.


--------------------
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
scrambled23
post Nov 20 2006, 03:28 AM
Post #22


Rank #1: Newbie
*

Group: Members
Posts: 16
Joined: 11-November 06
Member No.: 5,455



So like this?

CODE
    if( !empty( $_GET['id'] ) )
    print_r( $_REQUEST );
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Alis Dee
post Nov 20 2006, 04:39 AM
Post #23


Rank #4: Advanced
****

Group: Members
Posts: 127
Joined: 22-November 05
Member No.: 3,379



Almost. In this case, put it above the if-statement, otherwise you'll get more parsing issues because you've disconnected the if from the code-block it's supposed to be working on (since it isn't delimited by curly braces and is thus only going to look at the next satement immediatley down from it).

The other option, if you want to put it inside the if-block, is re-writing that part to include braces:

CODE
if( !empty( $_GET['id'] ) ) {
      $sql = "SELECT * FROM `php_blog` ORDER BY timestamp DESC LIMIT 1";
      print_r( $_REQUEST );
    } else
      $sql = "SELECT * FROM `php_blog` WHERE id = '$_GET[id]'";


--------------------
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
scrambled23
post Nov 20 2006, 10:18 PM
Post #24


Rank #1: Newbie
*

Group: Members
Posts: 16
Joined: 11-November 06
Member No.: 5,455



Thanks

Nothing shows up on the first page... just a backdated entry I didn't write. Clicking next leads to ?id=1 and there is
QUOTE
Array ( [id] => 1 )


From there, clicking previous leads to ?id=4 and the link stops working. It just goes to the same page.

What do I do now?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Alis Dee
post Nov 21 2006, 02:15 AM
Post #25


Rank #4: Advanced
****

Group: Members
Posts: 127
Joined: 22-November 05
Member No.: 3,379



Look very, very carefully at your code. Where are you using $_GET['id']? What value do you expect $_GET['id'] to be? Is it the value you were expecting? If not, why not?

You're not getting the expected value on your main page? Why not? Where are you getting this data from?

I'll give you one more hint; there is one single character wrong, and it's appeared twice on this page alone (and yeah, it's my fault), but that one character reverses the entire meaning of where it's used. In fact, now that I think about it, it's highly possible you might not even know what it's doing or what it means; so look through the code again, is there anything you don't understand? Make a note and ask Google; Google (and php.net) are invaluable tools and, unlike me, don't give annoying obtuse answers.

Almost there; happy hunting.


--------------------
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Jaroo
post Nov 21 2006, 02:38 AM
Post #26


We weren't crazy!
*****

Group: Loyal Members
Posts: 2,774
Joined: 24-February 04
From: Illinois
Member No.: 832



Or you could just give them the answer and explain why it is happening instead of sending them on a wild goose hunt?

I get that you're trying to walk this person through this but sometimes when you have no idea what you are looking for, you will never find it even with hints.

Just a thought!
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
scrambled23
post Nov 21 2006, 03:18 AM
Post #27


Rank #1: Newbie
*

Group: Members
Posts: 16
Joined: 11-November 06
Member No.: 5,455



That would be easier, as this has taken well over a week... but I suppose I have to learn it all eventually, so why not now.

Sounds like a semi-colon. If one was misplaced, wouldn't the script not work at all?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Alis Dee
post Nov 21 2006, 05:33 AM
Post #28


Rank #4: Advanced
****

Group: Members
Posts: 127
Joined: 22-November 05
Member No.: 3,379



QUOTE(Jamie @ Nov 21 2006, 01:38 PM) *
Or you could just give them the answer and explain why it is happening instead of sending them on a wild goose hunt?

I get that you're trying to walk this person through this but sometimes when you have no idea what you are looking for, you will never find it even with hints.

Just a thought!


Because, quite frankly, the error is an extremely basic (and extremely common) logic error, and the fact that scrambled23 can't find it is telling me that they've got next to no knowledge about really, really basic programming skills. The last thing the internet needs is yet another person copying half a (very insecure) tutorial with no real understanding of what's going on in their code, just so they can gloat that they 'wrote their own blog'.

I'm all for people learning to code, and believe me I've been there myself and I know how frustrating it is (the first night I sat down to write my own blog I actually cried in desperation thinking I'd never understand any of it), but the fact that the vast majority of posts in this forum are people handing out answers to already solved problems over and over again isn't helping anyone. If someone can't even be bothered searching the forum for the answer to the bloody register_globals problem, should they really be "writing their own blog script"? You want to learn to code? Great, I'm all for that and I'm all for helping people with their code so long as they can give me at least half an answer about what they think is going wrong. But -- maybe this is a newsflash -- you're not going to learn to code by reading the BAB 'tutorial'; you're going to need to go to some outside sources and get some basics on control structures, expressions, variables and operators.

Harsh? Elitist? Both; but I worked hard to learn what I know (which admittedly isn't much), and so did every other person I know who codes. I also know, from firsthand experience, that spoonfeeding doesn't work. I remember one uni assignment we were asked to (and this was the whole assignment spec) "Solve the producer-consumer problem in Nachos". This is a pretty common CompSci 201 kind of assignment. I had no idea where to even begin; I didn't even understand what the producer-consumer problem was (the entire lecture notes on it included a diagram of a segmented doughnut with an arrow going in one side and another arrow coming out the other side). So I Googled the question, copied some code, handed it in and passed my assignment. Do I know what the producer-consumer problem is? Vaguely, now. Do I know how to 'solve' it? Hell no; I didn't understand the solution then and I still don't. But my problems with the producer-consumer problem wasn't just about the question itself; it was about a fundamental problem suffered by pretty much every student at my uni, and that was lack of a decent grounding in programming basics. Not just the syntax stuff, but fundamental archetectural concepts.

And that's what I'm seeing here. Copying code without understanding the hows and whys of what you're doing isn't 'learning to code', and won't help you solve your own producer-consumer problems when you encounter them. And yeah, this kind of attitude is going to turn a lot of people off; good, I'd much rather people turn away and go back to WordPress (which is good logware), than hear another sob story about how someone's database got hacked or (worse) they got kicked off their host for using dodgy 'homebrew' scripts they didn't really understand.

But maybe that's just me. Feel free to jump in with the answer at any time. [shrugs]


--------------------
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Angela Maria
post Nov 21 2006, 07:33 AM
Post #29


Enthusiast
Group Icon

Group: CodeGrrl Staff
Posts: 1,109
Joined: 18-November 03
From: Philippines
Member No.: 518



QUOTE(Loqia Dee @ Nov 21 2006, 05:33 AM) *
I also know, from firsthand experience, that spoonfeeding doesn't work.

No, spoonfeeding doesn't work, but making things so terribly hard for the newbies to "get it" will more likely turn them off completely than get them more interested in how things work. If they get interested enough then they'll try to learn more. If they get discouraged because a seemingly simple problem is taking them a couple of weeks to figure out with someone else's help (who apparently knows more) then I don't think they'd even bother in the future.

My two cents as well. (This, from someone who learned PHP on her own.)


--------------------
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Amelie
post Nov 21 2006, 09:16 AM
Post #30


(Not the film)
Group Icon

Group: CodeGrrl Staff
Posts: 5,296
Joined: 14-January 05
From: UK
Member No.: 2,051



FYI: The security of the Build-A-Blog tutorial has been greatly increased, as well as it working with register_globals off.

Scrambled23 - what does your entire code look like now?


--------------------
-- Amelie

» N-N.net | RNM.com | NS.net
» Scripts | PHPAskIt fan?

"This is my signature. There are many others like it but only this one is mine."
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
scrambled23
post Nov 21 2006, 12:54 PM
Post #31


Rank #1: Newbie
*

Group: Members
Posts: 16
Joined: 11-November 06
Member No.: 5,455



This is my code
CODE
<?php
    // establish the db connection
    mysql_connect ('localhost', 'OMITTED', 'OMITTED');
    mysql_select_db ('OMITTED');

    // query to pull the post info out
    // this is either going to be specified by the query string, or else the most recent post
if( !empty( $_GET['id'] ) ) {
      $sql = "SELECT * FROM `php_blog` ORDER BY timestamp DESC LIMIT 1";
      print_r( $_REQUEST );
    } else
      $sql = "SELECT * FROM `php_blog` WHERE id = '$_GET[id]'";
    $result = mysql_query( $sql )
        or print( "Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error() );
    
    // only ever going to be returning one row, so we don't need a while() loop
    $row = mysql_fetch_array( $result );

    // format the data
    $date = date( "l F d Y", $row['timestamp'] );
    $title = stripslashes( $row['title'] );
    $entry = stripslashes( $row['entry'] );

// now print the entry
?>

    <p><strong><?=$title?></strong><br /><br />
    <?=$entry?><br /><br />
    Posted on <?=$date?>
    <hr /></p>

<?php
// write the 'previous' link
$sql_prev = "SELECT id FROM `php_blog` WHERE id < '$row[id]' ORDER BY id DESC LIMIT 1";
$result_prev = mysql_query( $sql_prev )
    or print( "Can't select previous entry id table php_blog.<br />" . $sql_prev . "<br />" . mysql_error() );
$prev = mysql_fetch_array( $result_prev );

// if 'id' is set, print a link
// else just print the word previous
if( isset( $prev['id'] ) )
    print "<a href=\"index.php?id=". $prev['id'] ."\">Previous</a> -- ";
else
    print "Previous -- ";

// now do the same with the 'next' link
$sql_next = "SELECT id FROM php_blog WHERE id > '$row[id]' ORDER BY id LIMIT 1";
$result_next = mysql_query( $sql_next )
    or print( "Can't select next entry id table php_blog.<br />" . $sql_next . "<br />" . mysql_error() );
$next = mysql_fetch_array( $result_next );

if( isset( $next['id'] ) )
    print "<a href=\"index.php?id=". $next['id'] ."\">Next</a>";
else
    print "Next";

?>
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Jaroo
post Nov 21 2006, 01:44 PM
Post #32


We weren't crazy!
*****

Group: Loyal Members
Posts: 2,774
Joined: 24-February 04
From: Illinois
Member No.: 832



QUOTE(Loqia Dee @ Nov 20 2006, 11:33 PM) *
QUOTE(Jamie @ Nov 21 2006, 01:38 PM) *
Or you could just give them the answer and explain why it is happening instead of sending them on a wild goose hunt?

I get that you're trying to walk this person through this but sometimes when you have no idea what you are looking for, you will never find it even with hints.

Just a thought!



Copying code without understanding the hows and whys of what you're doing isn't 'learning to code', and won't help you solve your own producer-consumer problems when you encounter them. And yeah, this kind of attitude is going to turn a lot of people off; good, I'd much rather people turn away and go back to WordPress (which is good logware), than hear another sob story about how someone's database got hacked or (worse) they got kicked off their host for using dodgy 'homebrew' scripts they didn't really understand.

But maybe that's just me. Feel free to jump in with the answer at any time. [shrugs]


Actually that is PRECISELY how I learned code and I had Amelie, Jenny and others not just give me answers but explaining WHY I was getting such errors. Did I post EVERY time I got an error? Of course not but when there was one I couldn't figure out, I came on here and got my solution and understood better because of it.

I'm obviously no expert with coding and I have a lot ot learn but I know if the people helping me did what you are doing, I would have given up because like I said, if you have no idea what you are looking for, you will never find the problem.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Jem
post Nov 21 2006, 02:38 PM
Post #33


Rank #5: Obsessed
Group Icon

Group: CodeGrrl Staff
Posts: 1,438
Joined: 23-February 05
From: England, UK
Member No.: 2,265



QUOTE(Angela Maria @ Nov 21 2006, 07:33 AM) *

If they get discouraged because a seemingly simple problem is taking them a couple of weeks to figure out with someone else's help (who apparently knows more) then I don't think they'd even bother in the future.

Bollocks. It took me months to learn how to use foreach() because I just didn't get it (God only knows why, looking back) and no matter how many times I looked at example code and spoonfed instructions I just couldn't understand. It wasn't until Karl (who has no knowledge of PHP, just a logical brain) sat down and forced me to look at it properly - to use my brain - that I finally understood. If people can't be arsed to get their head around the simple problems (as you put it) then they shouldn't be "programming" anyway.. it's those that just give up that end up releasing shoddy scripts or being "hacked".

I've seen more learning in this one CodeGrrl thread than in 99% of the others and that's because Loqia Dee has used the "find it for yourself" technique. While I don't always approve with dropping newbies in at the deep-end and I don't always agree with Loqia Dee, scrambled23 has picked up a lot of stuff pretty bloody quickly and I don't think they'd have managed that if one of us had turned around and said "here's the answer" - sure, it'd have solved the problem, but it wouldn't help several days/weeks/months down the line. We'd have just ended up with yet another thread asking the same question, worded slightly differently just because someone couldn't take the code out of the original context because they didn't understand it in the first place.

As scrambled23 said "but I suppose I have to learn it all eventually, so why not now." ...

I can see the problem and it was the first thing I noticed when I loaded this thread... Loqia is right: it's a single character that will affect the entire meaning of the block of code. It's that kind of immediate "aha" that scrambled23 will learn by knowing the code, the logic, etc instead of just being presented with a fix-all solution.

@scrambled23: if you're still stuck, try reading the code aloud or writing it down "logically" - i.e. Pseudocode (used to bug the crap out of me in college, but is incredibly handy for going over stuff). Of course, for this technique to work you're going to need to know exactly what each character means in the code.


--------------------
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Amelie
post Nov 21 2006, 03:09 PM
Post #34


(Not the film)
Group Icon

Group: CodeGrrl Staff
Posts: 5,296
Joined: 14-January 05
From: UK
Member No.: 2,051



Yes, I see the problem now too.

I agree that spoonfeeding is not necessarily the answer to all problems. When I help someone, I always try to tell them WHY what they had wasn't working, and what they need to do to fix it, and why that will work compared to what didn't. I think the Build-A-Blog tutorial goes through a lot of the code and tells people what it does and why - it's just that a lot of people just copy and paste the end example without reading about what the different sections of code mean to the script.


--------------------
-- Amelie

» N-N.net | RNM.com | NS.net
» Scripts | PHPAskIt fan?

"This is my signature. There are many others like it but only this one is mine."
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Jaroo
post Nov 21 2006, 03:17 PM
Post #35


We weren't crazy!
*****

Group: Loyal Members
Posts: 2,774
Joined: 24-February 04
From: Illinois
Member No.: 832



My issue here is, admittedly, Loqia Dee said the error was on HER part, yet she's trying to make the person she's trying to help, search relentlessly for it. I'm not sure I agree that this is helpful.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Jem
post Nov 21 2006, 03:25 PM
Post #36


Rank #5: Obsessed
Group Icon

Group: CodeGrrl Staff
Posts: 1,438
Joined: 23-February 05
From: England, UK
Member No.: 2,265



QUOTE(Jamie @ Nov 21 2006, 03:17 PM) *

My issue here is, admittedly, Loqia Dee said the error was on HER part, yet she's trying to make the person she's trying to help, search relentlessly for it. I'm not sure I agree that this is helpful.

Indeed, but if the person wants to use the code, they should be able to understand it. It's helpful in that it'll teach scrambled23 how to spot the error in the future and how to avoid making it.


--------------------
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Angela Maria
post Nov 21 2006, 04:39 PM
Post #37


Enthusiast
Group Icon

Group: CodeGrrl Staff
Posts: 1,109
Joined: 18-November 03
From: Philippines
Member No.: 518



QUOTE(Jem @ Nov 21 2006, 02:38 PM) *
QUOTE(Angela Maria @ Nov 21 2006, 07:33 AM) *

If they get discouraged because a seemingly simple problem is taking them a couple of weeks to figure out with someone else's help (who apparently knows more) then I don't think they'd even bother in the future.

Bollocks. It took me months to learn how to use foreach() because I just didn't get it (God only knows why, looking back) and no matter how many times I looked at example code and spoonfed instructions I just couldn't understand.


Not everyone has that kind of tenacity. Personally, I can go on weeks trying to understand something in my code, and I believe there are many others who can. I believe there are also others who can be "spoonfed" a quickfix and then they'll take it upon themselves to learn more. I'm just... concerned, I suppose, with the way Loqia was going about "teaching" scrambled23.

If I had professors like that back in college who seemed to try to point out to everyone that there was something wrong with my logic/way of thinking and that I'm lacking in that regard which is why I can't fix some small problem... *shrug* I didn't attend their class, plain and simple, and learned the course on my own. And yes, I passed the courses.

Don't get me wrong, I am definitely ALL for people being more logical and be able to troubleshoot their code on their own. Believe me, I want that very much. I'm not very well-versed in teaching, either, either offline or online, and I'm not pretending that I am.

But if one is teaching, they don't need to be so condescending.


--------------------
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Vixx
post Nov 21 2006, 08:19 PM
Post #38


CodeGrrl Administrator
Group Icon

Group: Admin
Posts: 2,421
Joined: 31-December 03
From: UK
Member No.: 657



Thanks for your interesting points, guys, but I'd be grateful if we could please stay on topic and stick to assisting scrambled's queries. Any other comments can be taken over to PMs.

Thanks in advance.


--------------------
V xx

I think. I play. I give. I love. I create. I am.

Geek Goddess Design: Web/Blog Design With A Touch of Geek Chic!!
Hosting with Dreamhost - use the Promo Code CODEGRRLPROMO and save $50!
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
scrambled23
post Nov 21 2006, 11:09 PM
Post #39


Rank #1: Newbie
*

Group: Members
Posts: 16
Joined: 11-November 06
Member No.: 5,455



Ok, so what are you guys saying I should do? I've been through the tutorials line by line, tried php.net and still don't know what this error is...
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Amelie
post Nov 22 2006, 09:52 AM
Post #40


(Not the film)
Group Icon

Group: CodeGrrl Staff
Posts: 5,296
Joined: 14-January 05
From: UK
Member No.: 2,051



Ok, the problem is here:

CODE
if( !empty( $_GET['id'] ) ) {
      $sql = "SELECT * FROM `php_blog` ORDER BY timestamp DESC LIMIT 1";
      print_r( $_REQUEST );
    } else
      $sql = "SELECT * FROM `php_blog` WHERE id = '$_GET[id]'";
    $result = mysql_query( $sql )
        or print( "Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error() );


That first line should not have a ! in it. The ! means "not", i.e. if $_GET[id] ($_GET[id] is the value from the query string, e.g. entry.php?id=1) is not empty, do something. That is incorrect since the code to be executed if $_GET[id] IS empty, relies on there being a value in $_GET[id]. Does that make sense?

You need the code to read "if $_GET[id] is empty, get the last added value from the database. Otherwise, get the value from the database which corresponds to $_GET[id]."

The code should look like this:

CODE
if( empty( $_GET['id'] ) ) {
      $sql = "SELECT * FROM `php_blog` ORDER BY timestamp DESC LIMIT 1";
      print_r( $_REQUEST );
    } else
      $sql = "SELECT * FROM `php_blog` WHERE id = '$_GET[id]'";
    $result = mysql_query( $sql )
        or print( "Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error() );


(And you can remove print_r( $_REQUEST ); - that is for debugging purposes only.)

Let me know if what I said is too confusing or you don't understand it. smile.gif


--------------------
-- Amelie

» N-N.net | RNM.com | NS.net
» Scripts | PHPAskIt fan?

"This is my signature. There are many others like it but only this one is mine."
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

3 Pages V < 1 2 3 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

- Lo-Fi Version Time is now: 29th July 2010 - 11:32 AM