• 0

How to make PHP subpages with ID?


Question

Recommended Posts

  • 0

You could try something like this:

<?php

switch($_GET['id'])
{
case 1:
include 'mypage.html';
break;
case 2:
include 'mysecondpage.html';
break;
default:
include 'home.html';
}

?>

Just add more cases to the switch to include more files and IDs, or you could use a database with the IDs in it and page references to include.

Smctainsh

  • 0

?php

switch($_GET['id'])
{
case 1:
include 'mypage.php';
break;
case 2:
include 'mysecondpage.php';
break;
default:
include 'home.php';
}

?>

OK.. so i put this in index.php..

for the links to work..

<a href="mypage.php?id=1">My page</a>

<a href="mysecondpage.php?id=2">Second Page</a>

???

  • 0
  NYU_KID said:
?php

switch($_GET['id'])
{
case 1:
include 'mypage.php';
break;
case 2:
include 'mysecondpage.php';
break;
default:
include 'home.php';
}

?&gt;

OK.. so i put this in index.php..

for the links to work..

<a href="mypage.php?id=1">My page</a>

<a href="mysecondpage.php?id=2">Second Page</a>

???

if you pasted that code in your index.php file, all of the links should be:

index.php?id=1 (to call mypage.php)

index.php?id=2 (to call mysecondpage.php)

etc.

  • 0

This is how I'd do it:

1) index.php

&lt;?php
$query = $_GET['p'];

$query = strtolower($query);
$space = array(' ','%20');
$path = str_replace($space,'_',$query);

if (!file_exists('./content/'.$path.'.php')) {$path = '404'; $title = 'Error 404';}		// Error 404

require_once('./source/template.php');
?&gt;

template.php

&lt;!--html here--&gt;&lt;?php include './content/'.$path.'.php'; ?&gt;&lt;!-- more hml --&gt;

where the include is the middle section aka content portion of your page.

  • 0
  NYU_KID said:
?php

switch($_GET['id'])
{
case 1:
include 'mypage.php';
break;
case 2:
include 'mysecondpage.php';
break;
default:
include 'home.php';
}

?&gt;

OK.. so i put this in index.php..

for the links to work..

<a href="mypage.php?id=1">My page</a>

<a href="mysecondpage.php?id=2">Second Page</a>

???

OK, I got everything to work.. but somehow.. when i click on mypage.php?id=1.. it doesn't replace the content from mypage.php ... ???

post-141808-1169088981_thumb.jpg

  • 0

you should be clicking on a link for index.php?id=1, not mypage.php?id=1

also, in the index.php file that you're using for this, there should be no content, only structural objects. all of the content is stored inside of the separate php files like mypage.php, mysecondpage.php, and home.php.

i assume that you just copied and pasted the code we gave you after some content in your existing index.php file. you can't do that.

  • 0

I actually wanted to make it for my blog pages.. without using wordpress..

ex. blog.php?id=1 (for january 17, 2007), blog.php?id=2 (for january 18, 2007)..

so i should leave blog.php empty.. with the php code and structural objects?

Edited by NYU_KID
  • 0

here are the bare essentials what you should have in your blog.php file for it to retrieve a specific blog entry:

&lt;?php
// Define the id you want to retrieve from the url
$id = $_GET['id'];

// Build the mysql query and corresponding array of retrieved information
$query = mysql_query("SELECT * FROM blogTable WHERE id='$id'");
$entry = mysql_fetch_row($query);

/* Here's where you would escape out of php and back into it
in order to format things for you */
?&gt;

one thing i STRONGLY recommend is to create a function that has all of the formatting information in it that will take the different parts of the mysql row as arguments. this way, you can use a loop to call the function several times, once per row, and have a list of blog entries. trust me, this will help a lot later on as you develop your script.

  • 0
  CeruleanCowboy said:
here are the bare essentials what you should have in your blog.php file for it to retrieve a specific blog entry:

&lt;?php
// Define the id you want to retrieve from the url
$id = $_GET['id'];

// Build the mysql query and corresponding array of retrieved information
$query = mysql_query("SELECT * FROM blogTable WHERE id='$id'");
$entry = mysql_fetch_row($query);

/* Here's where you would escape out of php and back into it
in order to format things for you */
?&gt;

one thing i STRONGLY recommend is to create a function that has all of the formatting information in it that will take the different parts of the mysql row as arguments. this way, you can use a loop to call the function several times, once per row, and have a list of blog entries. trust me, this will help a lot later on as you develop your script.

I would love to create a function that has all the formatting information in it.. but that is also complex.. for me, a beginner working with php language. I don't even get the bare essential code for my blog.php :(

  • 0
  CeruleanCowboy said:
what parts of it don't you get? i'll see if i can't explain them.

OK, that code you gave me, I put it into my blog.php file.. without editing any parts of the code? and how do i retrieve past entries using that code? How do i set up a function and loop it?

the other code..

&lt;?php

switch($_GET['id'])
{
case 1:
include 'blog.php';
break;
case 2:
include 'blog2.php';
break;
default:
include 'index.php';
}

?&gt;

this seems easier?

also.. can someone give me different examples of scroll bar types? except from the regular ones?

..and last..

is there a different between ?page=blog and blog.php?id=1 ??

THANKS AGAIN guys.

  • 0
  CeruleanCowboy said:
here are the bare essentials what you should have in your blog.php file for it to retrieve a specific blog entry:

&lt;?php
// Define the id you want to retrieve from the url
$id = $_GET['id'];

// Build the mysql query and corresponding array of retrieved information
$query = mysql_query("SELECT * FROM blogTable WHERE id='$id'");
$entry = mysql_fetch_row($query);

/* Here's where you would escape out of php and back into it
in order to format things for you */
?&gt;

one thing i STRONGLY recommend is to create a function that has all of the formatting information in it that will take the different parts of the mysql row as arguments. this way, you can use a loop to call the function several times, once per row, and have a list of blog entries. trust me, this will help a lot later on as you develop your script.

  golfantastic said:
OK, that code you gave me, I put it into my blog.php file.. without editing any parts of the code? and how do i retrieve past entries using that code? How do i set up a function and loop it?

the other code..

&lt;?php

switch($_GET['id'])
{
case 1:
include 'blog.php';
break;
case 2:
include 'blog2.php';
break;
default:
include 'index.php';
}

?&gt;

this seems easier?

also.. can someone give me different examples of scroll bar types? except from the regular ones?

..and last..

is there a different between ?page=blog and blog.php?id=1 ??

THANKS AGAIN guys.

first, i'll dissect this:

&lt;?php
/* $_GET is a superglobal associative array that retrieves values from the url. So, let's say that the url is as follows:
http://www.yoursite.com/blog.php?entry=1&date=23

$_GET would have the following values:
$_GET['entry'] == 1
$_GET['date'] == 23

It retrieves all values from the url after the question mark. Each variable and it's value are separated by an =, and each set is separated by an &amp;.

$_POST is the same kind of associative superglobal, only it doesn't retrieve values from the url, only server-side sent information, like from an html form.
*/

// Define the id you want to retrieve from the url
$id = $_GET['id'];

/*
mysql_query() is a function that will generate a mysql query to manipulate your database. You'll need to look up stuff on mysql and all of the commands and syntax because it's too numerous to fit into any single post.

mysql_fetch_row() is a function that returns an array starting at index 0 with each column. Let's say that you have a table with the following columns: name, phone, age; and one row containing the information John, 1234567, 24.

By using mysql_query() to create a query to find the information, and mysql_fetch_row() to assign that data to an array, you would get this.

$query = mysql_query("SELECT name, phone, age FROM users");
$data = mysql_fetch_row($query);

$data[0] == "John";
$data[1] == 1234567;
$data[2] == 24;
*/

// Build the mysql query and corresponding array of retrieved information
$query = mysql_query("SELECT * FROM blogTable WHERE id='$id'");
$entry = mysql_fetch_row($query);

/* Here's where you would escape out of php and back into it
in order to format things for you */
?&gt;

for the function, you'd have something like this:

function blogEntryFormat(title, message){
  ?&gt;

&lt;div id="title"&gt;&lt;?php echo $title; ?&gt;&lt;/div&gt;
&lt;div id="message"&gt;&lt;?php echo $message; ?&gt;&lt;/div&gt;

  &lt;?php
}

to loop through all entries in the table and output all of the messages with that function, you'd do this:

$query = mysql_query("SELECT title, message FROM blog");
while($data = mysql_fetch_row($query)){
   blogEntryFormat($data[0], $data[1]);
}

the while loop will continue to retrieve individual rows and perform the operations inside of the loop until there are no more rows left to analyze based on the query you entered.

if you're looking ONLY for a way to use one template file to generate other pages for your site, like if you have a page for your portfolio, one for home, and one for links, and all have the same design and html structure, you would use the switch command from the other page. you'd just take whatever content is on each page and put it into a corresponding file. it would look like this:

&lt;div id="mainContent"&gt;
&lt;?php
$page = $_GET['page'];
switch($page){
   case "portfolio": include("portfolio.php"); break;
   case "links": include("links.php"); break;
   default: include("home.php"); break;
}
?&gt;
&lt;/div&gt;

if someone went to the url http://www.yourpage.com/index.php?page=links then whatever is in the links.php page would be output. but if someone went to just http://www.yourpage.com/index.php, then home.php would be output.

  • 0
  CeruleanCowboy said:
yes, you would, but you shouldn't have that many.

by the way, let me ask now, what are you actually trying to do?

on my blog.php, i want to be able to have only my most recent 2 entries displayed on blog.php file..and then older entries on blog.php?id=1,2,3 so i can click 'BACK" or "NEXT" to my older or newer entries..

such.. click on BACK.. to blog.php?id=2 .. or NEXT to blog.php?id=1 ...

i dont really need the subpages on my index.php or any other pages... just focusing on blog.php

i'm reading http://www.php-mysql-tutorial.com/index.php for help as of now..and still a bit confused.

post-141808-1169181058_thumb.jpg

  • 0

here's what your code should be.

&lt;?php
if(isset($_GET['id'])){

  // Get and output the entry of the id in the url
  $id = $_GET['id'];
  $query = mysql_query("SELECT * FROM blog WHERE id = '$id'");
  $data = mysql_fetch_assoc($query);
  blogFormat($data['title'], $data['message'], whatever other columns you want to output);

  // Check for first and last entries
  $query = mysql_query("SELECT id FROM blog ORDER BY id DESC LIMIT 1");
  $data= mysql_fetch_row($query);
  $last = $data[0];
  $query = mysql_query("SELECT id FROM blog ORDER BY id ASC LIMIT 1");
  $data= mysql_fetch_row($query);
  $first = $data[0];

  // Output Back and Next if the entry the user is looking at is less than the latest id and greater than the first
  if($id &gt; $first){ echo "&lt;a href=\"blog.php?id=".$id-1."\">Back</a>&lt; $last){ echo "&lt;a href=\"blog.php?id=".$id+1."\">Next</a>

// Output the latest two entries if no entry is specified in the url
$query = mysql_query("SELECT * FROM blog ORDER BY id DESC LIMIT 2");
while($data = mysql_fetch_assoc($query)){
  blogFormat($data['title'], $data['message'], whatever other columns you want to output);
}
?&gt;

  • 0

mine's just a short script to retrieve a blog entry based on an id in the url or the 2 latest entries.

the link you just provided is for an entire class that will process all of the blog entries and do quite a bit more. i wouldn't touch it since you're new to php.

  • 0
  CeruleanCowboy said:
did you just copy and paste the code verbatim?

things like this "whatever other columns you want to output" shouldn't be in there.

do you think you could paste the entire code of your blog.php file in this thread?

&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
&lt;HEAD&gt;
&lt;title&gt;Blog&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="style.css">t;!--[if IE 7]&gt;
&lt;link rel="stylesheet" type="text/css" href="iestyle.css" media="screen" /&gt;
&lt;![endif]--&gt;

&lt;/HEAD&gt;

&lt;body id="home"&gt; 
&lt;div id="col1i"&gt;
&lt;img src="/header56.jpg" alt="header"&gt;
&lt;/div&gt;

&lt;div id="col2i"&gt;
&lt;img src="/navigation7.jpg" alt="navigation"&gt;
&lt;table bgcolor="#ffffcc" width=238px height=21px&gt;
  &lt;tbody&gt;
	&lt;tr&gt;
			&lt;td align=center&gt;
&lt;a href="/contact.php">Contact</a>;a href="/blog.php">Blog</a>;a href="/"&gt;Home&lt;/a&gt;

&lt;/td&gt;
	&lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;br&gt;
&lt;?php
if(isset($_GET['id'])){

  $id = $_GET['id'];
  $query = mysql_query("SELECT * FROM blog WHERE id = '$id'");
  $data = mysql_fetch_assoc($query);
  blogFormat($data['title'], $data['message'];

  $query = mysql_query("SELECT id FROM blog ORDER BY id DESC LIMIT 1");
  $data= mysql_fetch_row($query);
  $last = $data[0];
  $query = mysql_query("SELECT id FROM blog ORDER BY id ASC LIMIT 1");
  $data= mysql_fetch_row($query);
  $first = $data[0];

  if($id &gt; $first){ echo "&lt;a href=\"blog.php?id=".$id-1."\">Back</a>&lt; $last){ echo "&lt;a href=\"blog.php?id=".$id+1."\">Next</a>

$query = mysql_query("SELECT * FROM blog ORDER BY id DESC LIMIT 2");
while($data = mysql_fetch_assoc($query)){
  blogFormat($data['title'], $data['message'];
}
?&gt;

&lt;div id="blog"&gt;
&lt;table bgcolor=#ffffcc width=610px&gt;
  &lt;tbody&gt;
	&lt;tr&gt;
		  &lt;td&gt;
&lt;p&gt;dsa&lt;/p&gt;
&lt;ul id="navigation"&gt;
&lt;li class="left"&gt;&lt;a href="/"&gt; &lt;&lt; Back&lt;/a&gt;&lt;/li&gt;
&lt;li class="right"&gt;&lt;a href="#"&gt;Archives&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
	&lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • It’s encouraging that Intel is still competing, the best they can, in the gpu space.
    • Windows 11 KB5062233/ KB5060843/ KB5062197/ KB5061090 setup, recovery updates released by Sayan Sen This week Microsoft released non-security preview updates for Windows 11 22H2 and 23H2 under KB5060826 as well as for 24H2 under KB5060829. The company also published dynamic updates alongside them. Dynamic updates bring improvements to the Windows Recovery in the form of Windows Recovery Environment (WinRE) updates, also called Safe OS updates, as well as to the Setup binaries in the form of Setup updates. These Dynamic Update packages are meant to be applied to existing Windows images prior to their deployment. These packages include fixes to Setup.exe binaries, SafeOS updates for Windows Recovery Environment, and more. Dynamic Updates also help preserve Language Pack (LP) and Features on Demand (FODs) content during the upgrade process. VBScript, for example, is currently an FOD on Windows 11 24H2. Both setup and recovery updates were released. The changelogs are given below. First up we have the Setup updates: Up next, we have the recovery updates: Microsoft notes that the Recovery updates will be downloaded and installed automatically via the Windows Update channel. The Setup updates, however, need to be downloaded and installed manually. You can avail them on Microsoft's Update Catalog website: KB5062233, KB5060843, KB5062197, and KB5061090.
    • Gaming mouse review: Keychron Lemokey G2 8K Wireless, high performance, light in weight by Robbie Khan Available in the UK and USA, the G2 is a wireless gaming mouse by Lemokey, the customised gaming peripheral arm of Keychron, a brand many readers will know well. I've checked out Keychron keyboards recently, models such as the top-tier Q6 Max, and the bargain priced B6 Pro. I never got the chance to check out the G1, but the G2 seems to put itself firmly in-between competing 8K gaming mice out there whilst coming in at a decent sum under £100/$100. The other benefit is that it uses Keychron Launcher, the web-based software that has been a staple part of Keychron keyboards for a long time now. I rate the launcher highly and have rarely had a problem with it, so it is great to see a robust piece of software being used on an affordable gaming mouse that's not only built well, but performs well, too. As it's a high performance gaming mouse with a flagship PixArt sensor, I will be comparing the G2 directly against my personal mouse, the similarly sized Pulsar Feinmann F01. Both mice are priced wildly differently, so it was interesting to compared several factors between them to see if one bests the other where it matters most, or if it just comes down to individual preferences. Let's check out the specs... Keychron Lemokey G2 SKU G2-A1 Sensor PixArt 3950 IPS 750 MCU Realtek 8762G Connectivity Bluetooth 5.1 / 2.4 GHz / Wired (type-C cable) Polling Rate Up to 8000 Hz (2.4 GHz / wired mode), 125 Hz (Bluetooth mode) Motion Sync Yes Angle Snapping Yes Acceleration 50g Cable Detachable Type-C to C Cable + Type-A to Type-C adapter Dongle Micro USB-A dongle Main switches Huano Micro Switch Switch lifespan 80 Million Clicks Battery 300 mAh Battery life 60 Hours (Under 1000Hz) 35 Hours (1000-4000Hz) 20 Hours (4000-8000Hz) Skates Teflon / PTFE Lift Off Distance 0.7 / 1.0 / 2.0 mm Material (Body and Grip) ABS Dimensions 118mm / 62.6mm / 38.2mm Tracks on glass (min. 4 mm thickness) Yes (max 1000Hz report rate) Weight 52 ± 3 g Colours Black / White Price £73.99 / $69.99 Fit and finish Made of all ABS, it certainly doesn't feel anywhere near as nice as the F01, but then again it's priced much lower, so no complaints there. To me it feels more like a previous generation Endgame/Zowie mouse, before they started to use the more grippy textured finish. It's a fine finish but how long that lasts only time will tell. With the Endgame mice I always found that the side buttons would go shiny first, and I suspect the exact same to apply here with the G2, since the size and shape of them is similar, as is the material quality. Speaking of side buttons, my personal preference is flat and large buttons, the long and pointed ones like those on the G2 shown above and GAMIAC PX71 at the bottom always felt a bit awkward to me for naturally resting a thumb on, whereas the large surface area of the ones found on the Feinmann and others like it feel more ergonomic and comfortable for long usage sessions. Pulsar Feinmann F01 (ergo shape) Lemokey G2 (ambidextrous shape) Under my 19cm hand, the G2 feels comfortable and stable for both claw and palm grip styles, though my usual style is a hybrid approach. I like to pinch a mouse with a thumb and ring finger, then articulate forward or backward movement with them to adapt to different play-styles. Both mice have a sloping back hump that works brilliantly for this and i had no problem getting comfortable with the G2 here. The underside of the G2 hides a cool feature I wish more more mice makers adopted, can you spot what it is in the photo below? That little flap at the bottom to store the USB dongle, it's convenient, especially for gamers who travel with their mice or laptop gamers. The skates applied are PTFE and cover three zones on the underside, sadly I forgot to take a photo of them before swapping them out to my preferred skates, the Wallhack Pro UWMP yellow dots. I found no problem with glide resistance on the stock skates, though find dots glide nicer overall and these yellow dots slide around like butter. Lemokey states 52g for the weight, give or take 3g, so my scales measure up perfectly here. It's not as lightweight as the Feinmann which currently is 45g, but it's still considered super-light and honestly, under the hand, this weight difference on dot skates is hard to tell apart. Gripping the G2 hard on both sides shows no obvious issues with flexing, so the internal construction is also up to scratch and exactly what i would expect in this price range. The differences start to be noticed when you are using the G2 as a combination mouse, both in gaming and desktop use. The wheel is thinner than the one on the Feinmann and other gaming mice with fat wheels. I much prefer a nice wide wheel, the diameter is also much larger as can be seen with the overhead photo above. The button tops also have a distinct difference in feel when actuating the switches. there is more of a hollow feel to the G2's tops, as well as slightly more travel post-click. the Huano switches feel and sound excellent, though, but I think the ABS contact surface on the outside could have been slightly more distinct in feel. Here is a demo of how the main switches sound, you can also hear the switch tops clap if you pay attention: Likewise the side buttons have extra play after the switches actuate, something the Feinmann and others in the higher price category don't tend to have. Features & software Aside from the usual features that all gaming mice support these days, such as Motion Sync, lift off distance and Macro, the big feature with the G2 is that is uses Keychron Launcher, the web-browser based software. All changes are stored directly onto the on-board memory, here is what the sections within it look like: I found no bugs with the implementation here, and unlike other Keychron wireless devices I have used with Launcher in the past, the G2 connects and can be customised in it over both wired and wireless. Just be aware that the firmware update option when connected via wireless will only check the dongle's firmware. To check the G2's firmware, a USB cable will need to be connected. Performance Whether on the desktop in Windows or in games, the cursor and motion performance is excellent, though this is to be expected from all modern gaming mice, regardless of price. The higher priced mice tend to have better use of high quality materials, and implementation of software and physical features. I'm currently pacing through nine games, all mixed genre, and in each of them I had no troubles quickly getting comfortable with the G2. If you're used to an Endgame XM2we sized mouse, then this will be just as familiar to you. The convenience of the DPI button at the top of the mouse instead of underside makes instant switching simpler, although the button can be remapped to do something else for those who don't care about DPI toggling. Here is a demo of the G2 playing Doom: The Dark Ages: The sensor tracking performance at 8K was perfect, aside from the few moments my physical movement on the mousepad slowed down causing some drops in the Razer mouse tracking measurement graph below: A consistent 7900-8000Hz polling was observed, though keep in mind that using such high polling rates will impact CPU performance whilst gaming. How much this affects framerates will boil down to the CPU you have. On my i7 12700KF there was no change whilst gaming, but during the measurement above I did observe 13% CPU package utilisation. In practice, though, I saw no performance difference playing at 8K versus 2K or even 1K. Battery life will drastically be impacted at 8K polling rates for obvious reasons, and whilst many prefer 4K, there is a growing trend to just stick to 2K which feels the safe middle ground of great battery performance, whilst still being suitably responsive on paper for even the most fast-paced of gaming sessions. Conclusion The Lemokey G2 has proven to be a rather excellent mouse, not just for gaming, but general use, too. Though being excellent doesn't give it any special status, as there are equally excellent mice out there that cost the same, less and even more. A buying decision will come down to individual needs, do you value the use of a browser-based software tool like Keychron Launcher? If so, then this is right up your street. Do you want something lightweight but also supports the gaming features and feel in the hand as more expensive mice? Maybe the G2 will satisfy. It's not totally perfect, nothing ever is, the thumb buttons could have been a bit wider, and the switch caps have more travel after clicking than what I am comfortable with, but otherwise this is a great mouse with features that rival the competition. The sensors on gaming mice these days isn't a key selling factor any more either, since even entry level gaming mice are capable of precision tracking and speed that was only possible on the top-tier models of the past, like many things in tech now, the point of diminishing returns has been reached, and brands have to work harder at giving us consumers a unique selling point to attract interest. I believe the G2 has at least one USP (Keychron Launcher), it also helps that it's a very comfortable mouse to use for all-day sessions.
    • Delays can happen any time and names changed, but the joke was still there to be made.
  • Recent Achievements

    • Week One Done
      Alexander 001 earned a badge
      Week One Done
    • Week One Done
      icecreamconesleeves earned a badge
      Week One Done
    • One Year In
      PAC0 earned a badge
      One Year In
    • One Month Later
      PAC0 earned a badge
      One Month Later
    • One Year In
      Shahmir Shoaib earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      564
    2. 2
      +FloatingFatMan
      187
    3. 3
      ATLien_0
      185
    4. 4
      Skyfrog
      113
    5. 5
      Xenon
      110
  • Tell a friend

    Love Neowin? Tell a friend!