Forum Moderators: coopster

Message Too Old, No Replies

creating web pages on the fly using php

creating php pages on the fly

         

togethercomms

2:24 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



Hi everyone, i know you must get this all the time but most of the answers i found are very vague.

I am creating a CMS system for a client and the webpage is all positioned absolutly.

The client will be adding jobs on this like a job board and i want to make php add a new page whenever the amount of jobs is about to go over the web page height.

(e.g web page height is 660px and the amount of jobs exceeds this)

and also add a next button to the next page of results.

Hope this makes sense

jatar_k

2:34 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld togethercomms,

I am not sure you need to make another page. Do you just want "paging"? Meaning the next/previous/first/last type links so you can go through your results?

that would work, unless you actually want a permanent page, though you should then generate these pages in advance

togethercomms

2:42 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



I think i would need perminent pages as prespective job hunters would be looking through for a job, so they would need to be their until they got taken down.

How would i create a template and then php uses that template to create the next result page.

Many THanks for your reply

jatar_k

2:52 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the template would just be your standard page with no results

the steps involved would be something like

get results
figure out how many pages to make

while there are more results
-open file pointer
-add header to file
-add next bunch of results to file
-add footer to file
-close file

you would have to add in dynamic next links but that's pretty easy, you've already figured out how many pages there are and just build the set of links to the other pages

you could probably have some type of sitemap or index that had a link to every page, actually a db would be good.

the issue with this approach is that you need to make the destination directory writable, this is always a security issue

togethercomms

3:03 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



sorry, didn't get that?
it might be because i'm a little tired, is their a script example i can see?

mainly because i got slightly lost.

Many Thanks

jatar_k

3:26 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about this

you would do it exactly the same way as you would to output the page to the browser, except you would write to a file

togethercomms

4:14 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



yeah, sounds about right

togethercomms

8:05 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



right ok, sorry to keep going on.

I have created an insert script that inserts data to mysql and then i created the page which displays all the data in custom boxes and now all i need is when you insert new data it creates a new web page with the rest of that specific data.

e.g i insert data for a finance manager now i want a link that will go from the data display page to the full info page on just the finance manager.

Tommybs

7:07 am on Jun 24, 2009 (gmt 0)

10+ Year Member



I'd suggest creatign 2 pages. The first page is the list of the available jobs (list.php) and you implement pagination on this to display the number of jobs other how ever many pages it needs. Each job title could then be a link to another page (job.php) where you also pass a unique job_id relating to the database. job.php?id=2 .job.php then gets the id and selects the correct job from the database and displays all the information

togethercomms

8:19 am on Jun 24, 2009 (gmt 0)

10+ Year Member



so it is possible to do?
how would i pass the job_id to display on the other page?

Tommybs

8:42 am on Jun 24, 2009 (gmt 0)

10+ Year Member



using a $_GET parameter, making sure the input has appropriate checking.

Basically the link to the job page will be something like job.php?id=2

then on job.php code like


if(isset($_GET['id']) && ctype_digit($_GET['id'])){
$id = $_GET['id'];
//check this id exists in db
}else{
$id = 0;
}

if($id == 0){
echo "no such job";
}else{
//show correct job
}

togethercomms

8:46 am on Jun 24, 2009 (gmt 0)

10+ Year Member



it sounds so easy, so that code goes on the template file (job.php) and it will call the data from the mysql relating to the job that was just hit.

My god MySQL is beautiful

Tommybs

9:01 am on Jun 24, 2009 (gmt 0)

10+ Year Member



just make sure the link on the list page has the id from the job table

togethercomms

9:09 am on Jun 24, 2009 (gmt 0)

10+ Year Member



yh, ive made the id part of the title

Tommybs

9:39 am on Jun 24, 2009 (gmt 0)

10+ Year Member



be careful of using the ctype_digit line above then as it checks to make sure something is numeric which I'm guessing your title won't be. You should then run some validation/security on the $_GET value

togethercomms

9:44 am on Jun 24, 2009 (gmt 0)

10+ Year Member



validation/security?
How do you mean?

togethercomms

10:23 am on Jun 24, 2009 (gmt 0)

10+ Year Member



actually thinking about it, how would i go about making the link?

at the moment i have;
<a href='www.example.com/job/job-info$id.php'>More information</a>

[edited by: dreamcatcher at 3:03 pm (utc) on June 24, 2009]
[edit reason] use example.com. Thanks. [/edit]

Tommybs

10:28 am on Jun 24, 2009 (gmt 0)

10+ Year Member



<a href='www.example.com/job/job-info.php?id=$id'>More information</a>

[edited by: dreamcatcher at 3:03 pm (utc) on June 24, 2009]
[edit reason] use example.com. Thanks. [/edit]

Tommybs

11:38 am on Jun 24, 2009 (gmt 0)

10+ Year Member



just a bit more info on this:
lets assume you have a table called job in your db with the following structure:

job_id (PRIMARY KEY INT)
job_title (varchar 255)
job_description(text)
application_date_ends(datetime)

your list.php page would be something like

$q = @mysql_query("SELECT job_id,job_title FROM job ORDER by application_date_ends ");
while($r = @myqsl_fetch_array($q)){
echo "<p><a href=\"www.example.com/job/job-info.php?id=".$r['job_id']."\">".$r['job_title']."</a></p>";
}

The above is without any pagination work done to it but there are tutorial on the net on how to do this.

Next on your job-info.php page you would have something like


if(isset($_GET['id']) && ctype_digit($_GET['id'])){
$id = $_GET['id'];
$q = @mysql_query("SELECT 1 FROM job WHERE job_id = $id");
if(@mysql_num_rows($q) == 1){ // we have a match
$id= $id;
}else{
$id = 0;
}
}else{
$id = 0;
}

if($id == 0){
echo "<p>You have selected an invalid job. Please return to the job index</p>";
}else{
$q = @mysql_query("SELECT * FROM job WHERE job_id = $id");
//show results from DB including description and date this time
}

[edited by: dreamcatcher at 3:02 pm (utc) on June 24, 2009]
[edit reason] use example.com. Thanks. [/edit]

togethercomms

11:39 am on Jun 24, 2009 (gmt 0)

10+ Year Member



cheers, this is working like a dream.
Im using echo to try and display data
but im getting nothing.

Im sure i'm missing something but i cant for the life of me think what, i know it's a small thing.

<?php
$mysqli = mysqli_connect("#*$!x.#*$!x.co.uk", "#*$!#*$!#*$!2", "#*$!#*$!xx", "#*$!#*$!#*$!x");

if (mysqli_connect_errno()) {
printf("Connect Failed: %s\n", mysqli_connect_error());
exit();

} else {

if(isset($_GET['id']) && ctype_digit($_GET['id'])){
$id = $_GET['id'];
//check this id exists in db
}else{
$id = 0;
}

if($id == 0){
echo "no such job";
}else{
//show correct job
echo "<div id='border'>";
echo "<div class='bg'><div class='subject'><b>Ref: ".$id." - </b>&nbsp; &nbsp; &nbsp; &nbsp;".$title."</div> <div class='salary'>".$salary."</div> </div>";
echo "<div class='body'>".$short."</div>";
echo "<div class'info'><a href='template/job-info.php?id=$id'>More information</a></div>";
echo "<div class='bg'><div class='footer'><a href='mailto:info@example.com'>Click to respond</a></div> </div>";
echo "</div><br>";
}
}

?>

[edited by: dreamcatcher at 3:02 pm (utc) on June 24, 2009]
[edit reason] use example.com. Thanks. [/edit]

Tommybs

11:45 am on Jun 24, 2009 (gmt 0)

10+ Year Member



Are you just getting a blank screen or is it just not showing the variables?you don't have a query in the //show correct job section to pull back the infomation so your variables wont be populated.

togethercomms

12:16 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



it's displaying the "You have selected an invalid job. Please return to the job index"

that means it's either not finding the result or its not connecting to the table?

hmmm very hard this

Tommybs

12:34 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



Is this a problem on the list of all jobs or a single job page? Your code above looks like it should be the list page in which case you wouldn't be searching for a particular id.

togethercomms

12:45 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



this is the code i have put in to replace the one above

<?php
$mysqli = mysqli_connect("#*$!#*$!xx", "#*$!#*$!#*$!x", "#*$!#*$!#*$!", "#*$!#*$!#*$!x");

if (mysqli_connect_errno()) {
printf("Connect Failed: %s\n", mysqli_connect_error());
exit();
} else {

if(isset($_GET['id']) && ctype_digit($_GET['id'])){
$id = $_GET['id'];
$mysqli = @mysql_query("SELECT 1 FROM job_board WHERE id = $id");
if(@mysql_num_rows($mysqli) == 1){ // we have a match
$id = $id;
}else{
$id = 0;
}
}else{
$id = 0;
}
}

if($id == 0){
echo "<p>You have selected an invalid job. Please return to the job index</p>";

}else{

$mysqli = @mysql_query("SELECT * FROM job_board WHERE id = $id");

//show results from DB including description and date this time
}
?>

I don't know where i would be going wrong, i know that the main data shouldn't be coming up because i haven't asked for it but at least i should be getting is a blank screen and not the custom error.

Tommybs

12:57 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



You will as you should have the list page and specific jobs on separate pages. The code above defaults the value of $id to 0. If you want it all on 1 page then instead of "You have selected an invalid job. Please return to the job index" loop through all jobs here

Tommybs

12:58 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



the address bar for the job should show the id parameter in it as well

e.g does your url look like

job-info.php?id=23

try plugging a known id in replace of 23 and navigate to the page

togethercomms

1:07 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



this is what i have on the list-jobs.php;

if (mysqli_connect_errno()) {
printf("Connect Failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "SELECT * FROM job_board";
$res = mysqli_query($mysqli, $sql);

if($res) {
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$id = $newArray['id'];
$title = $newArray['title'];
$short = $newArray['short'];
$salary = $newArray['salary'];
$end_date = $newArray['end_date'];
echo "<div id='border'>";
echo "<div class='bg'><div class='subject'><b>Ref: ".$id." - </b>&nbsp; &nbsp; &nbsp; &nbsp;".$title."</div> <div class='salary'>".$salary."</div> </div>";
echo "<div class='body'>".$short."</div>";
echo "<div class'info'><a href=\"template/job-info.php?id=".$id."\">More information</a></div>";
echo "<div class='bg'><div class='footer'><a href='mailto:info@example.com'>Click to respond</a></div> </div>";
echo "</div><br>";
}
} else {
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
}
mysqli_free_result($res);
mysqli_close($mysqli);
}

this lists all the jobs from the mysql database, this works.
then the one above was supposed to display the specific data of a job after clicking on it in the "jop-info.php"

[edited by: dreamcatcher at 3:01 pm (utc) on June 24, 2009]
[edit reason] use example.com. Thanks. [/edit]

Tommybs

1:15 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



ok change the mysql_query statements in the job-info.php page I used to make use of mysqli_query like you have used elsewhere and see if that works

togethercomms

1:37 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



it's still not working...

togethercomms

3:46 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



right great i got it working now to where it states that it has found the record, woop woop.

It was a few errors in the mysql_query.

Now im just trying to get the info echo'd that it has found

This 32 message thread spans 2 pages: 32