July 3, 200818 yr Good day people of the forum. I am trying to improve on my php knowledge. I am trying to make my own blog so I can practice various things. I have an early site that I'm testing things on here. what I want to be able to do is create dynamic urls so I can show individual entries or all comments for an entry without having to create a new page every time. I can already add entries to the database and have that working as I want. Whenever I search for a good tutorial I can't really find what I want. can anyone help or guide me to somewhere useful? Thanks
July 3, 200818 yr Hi Mark, One way to create a "dynamic" page is to use a variable in your URL ... I'll just show a quick example: URL : website/page.php Page loads normally ... nothing fancy so far. URL : website/page.php?show_comments=1 A lot of possibilities (let's just say that the page loads normally + plus some comments show up) ... here's some code: //this is were the page loads normally //check to see if show_comments is set if ( isset( $_GET('show_comments') ) ) { //variable is set -- you'd probably want to check that your variable is an integer and it's equal to 1 if ( is_int( $_GET('show_comments') ) && $_GET('show_comments') == 1 ) { //this is were you could do a lot of stuf ... show some comments ? } } Notice that in this situation I'm not even using an else statement (if show_comments is 1 comments will show up .. otherwise .. they don't). Showing individual entries (blog posts for example) takes a similar approach with URL : website/post.php?post_id=NUMBER. You can have multiple variables in your URL too: post.php?post_id=3&show_comments=1 I hope this is what you were asking for. Cheers
July 4, 200818 yr Author Thanks. Would I need to connect to the database and the table first above that code?
July 5, 200818 yr Thanks. Would I need to connect to the database and the table first above that code? You can for example include a php script (at the top of the scripts that need a db connection) that connects to mysql and selects the database (i usually have only 1 database per website). Running specific queries (like selecting the comments) you'd do after the check (if needed that is). In the previous example that would be here: //this is were you could do a lot of stuf ... show some comments ? P.S. Sorry for the late reply.
July 10, 200818 yr Author sorry to be an idiot but I'm still not really getting it. This is what I have, it's probably really wrong. <?php $page_title = 'The List'; include ('./php/header.php'); ?> <h2>Latest Updates</h2> <?php $con = mysql_connect("localhost","******","******"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("roondog_news", $con); $result = mysql_query("SELECT * FROM listnews"); if ( isset( $_GET('storynum') ) ) if ( is_int( $_GET('storynum') ) && $_GET('storynum') == 1 ) { while($row = mysql_fetch_array($result)){ echo "<div class='info'>"; echo "<h3>".$row['challenge']."</h3><h4>".$row['published']."</h4>".$row['story']; echo "<p><a href='".$row['challenge_link']."'>View Challenge</a></p>"; echo "<p><a href='".$row['challenge_link']."/#comment'>Add Comment</a> | <a href='#nogo'>View Comments (".$row['comments_no'].")</a></p>"; echo "</div>"; } } } mysql_close($con); ?> <?php include ('./php/footer.html'); ?> I got it done, it makes me so happy when I get things sorted. It was the wrong type of brackets after $GET. I've also changed it a little. Thanks for the help.
Create an account or sign in to comment