/    Sign up×
Community /Pin to ProfileBookmark

Need help posting and formatting poems from database

Hi everyone,

I am looking for some assistance to accomplish the following task. I want to be able to have a list of poems on my website with a link to that specific poems page (I have managed to accomplish listing and link).

I also want to be able to have it so that when someone clicks on a poem, the poem shows up in “normal” poem formatting with stanzas and such. As of now, I have no idea how to get my poems to format properly. Right now my poems come out looking like this:

“Bells Rang
As you pull out of the driveway and you look to your right Your buddy has a wet face and the most intense look of fright Finished cranking the wheel and aiming the rig straight You lift your head and feel for a better view down the street in the day light The sights can only paralyze your thoughts as you were not trained for this No one is ever trained for loss and you can’t help but seeing it Your buddies aren’t saying anything and this has to be the most empty feeling As you drive in the wrong direction…”

Obviously this is unformatted and what I would like is for it to look like this:

Bells Rang

“As you pull out of the driveway and you look to your right
Your buddy has a wet face and the most intense look of fright
Finished cranking the wheel and aiming the rig straight
You lift your head and feel for a better view down the street in the day light

The sights can only paralyze your thoughts as you were not trained for this
No one is ever trained for loss and you can’t help but seeing it
Your buddies aren’t saying anything and this has to be the most empty feeling
As you drive in the wrong direction…”

So far, I have this coding established. The first script is to list my poems and the second script is supposed to do the listing of each poem and so on as I would like it to actually look. I have been tinkering based on internet searches and books I own. I realize the second script is completely off base, but I wanted to show what I have thrown together.

In advance, I greatly appreciate any of your time you can lend a stranger.

Thanks,
Randy

In case this is needed:
the table structure of my MySQL database:
id,
letter,
title,
poem

Script one: Poemindex.php

[CODE]
<?php
//LIST POEMS WITH LINKS
// database connection

// variable setting
$db_hostname = “localhost”; //Host
$db_username = “”; //Username
$db_pass = “”; //Password
$db_name = “”; //Database Name

/*MYSQL DATABASE CONNECTION/ TRACKING FUNCTIONS
————————————–*/
// connect to database
$dbh = mysql_connect ($db_hostname, $db_username, $db_pass) or die (‘I am unable to connect to the database because of: ‘ . mysql_error());
mysql_select_db ($db_name);

// select data from the database to use
$result = mysql_query(“SELECT * FROM poems ORDER BY title LIMIT 100”) or die(‘mysql_error()’ . ‘Error: ‘ . mysql_error() );
while($row = mysql_fetch_array($result))
{
echo “<a href=”poemview.php?title=”.$row[‘title’].”” target=”_blank”>”.$row[‘title’].”</a><br/>n”;
}
?>
[/CODE]

Script two: POEMVIEW.PHP
————————


[CODE]
<?php

//This page retrieves the name of the contact via $_GET() method. The value of $_GET is then processed by the query:

$db_hostname = “localhost”; //Host
$db_username = “@@@@”; //Username
$db_pass = “@@@@”; //Password
$db_name = “@@@@”; //Database Name

$dbh = mysql_connect ($db_hostname, $db_username, $db_pass) or die (‘I am unable to connect to the database because of: ‘ . mysql_error());
mysql_select_db ($db_name);

$result=mysql_query(“SELECT * FROM poems WHERE title = ‘”.$_GET[“title”].”‘”) or die(“Could not connect to database!”);
while($row=mysql_fetch_row($result))

{
//echo $row[1].”<br/>n”;
echo $row[2].”<br/>n”;
echo $row[3].”<br/>n”;
echo $row[4].”<br/>n”;
echo $row[5].”<br/>n”;
echo $row[6].”<br/>n”;
//Poem Title: echo $row[3]; <br /><br />
//Poem: echo $row[4];

// Write the data of the poem
//echo “<dt>Title:</dt><dd>” . $row[“title”] .”</dd>”;
//echo “<dt>Poem:</dt><dd>” . $row[“Poem”] . “</dd>”;

}
?>
[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@holyhttpMay 24.2012 — If the actual poem is defined by $row[4] then that's the data you should format.

A quick

nl2br($row[4])

will convert carriage return (new line breaks) into its HTML correspondant (<br>).

So, if for instance you used a plain text editor such as Notepad to format the layout of your poem then nl2br (new line to break) function should perform the job perfectly.
Copy linkTweet thisAlerts:
@tuxandpucksauthorMay 24.2012 — thanks for the info. I will give this a go. thanks again, randy.
×

Success!

Help @tuxandpucks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...