12 Replies - 686 Views - Last Post: 05 June 2012 - 07:47 AM Rate Topic: -----

#1 tuxandpucks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 12

Content Formatting Help Needed.

Posted 03 June 2012 - 04:40 PM

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.

I have seen and tried to fiddle with nl2br, sprintf and my issue is I don't know the proper syntax to use them yet.

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: Listing my poems. I am fine with this page for now.
<?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";
}
?>



Script Two: This is the script that I can't figure out.


<?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>";

}
?>




Is This A Good Question/Topic? 0
  • +

Replies To: Content Formatting Help Needed.

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 801
  • View blog
  • Posts: 1,689
  • Joined: 30-January 09

Re: Content Formatting Help Needed.

Posted 03 June 2012 - 05:00 PM

This will be more of a case of what the format of the data is in the database, whether is has line-breaks and carriage returns, or the poem is formatted in some other way.

Can you give an example of data from the DB, using a query like this:
SELECT *
FROM poems
LIMIT 3


That should get 3 poems from the DB, probably the first three that were entered.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Content Formatting Help Needed.

Posted 03 June 2012 - 05:18 PM

I would put the text from the poem in a div and format that using CSS.

Now for your big problem: security. You're trusting the user and that is bad. Use prepared statements to protect against SQL injection. Here's a good tutorial for PDO.
Was This Post Helpful? 0
  • +
  • -

#4 tuxandpucks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 12

Re: Content Formatting Help Needed.

Posted 03 June 2012 - 05:40 PM

View PostCTphpnwb, on 03 June 2012 - 05:18 PM, said:

I would put the text from the poem in a div and format that using CSS.

Now for your big problem: security. You're trusting the user and that is bad. Use prepared statements to protect against SQL injection. Here's a good tutorial for PDO.
u

As I am learning PHP and accessing my database, I have actually just read about SQL injection. I will certainly give it consideration once I can show my content.

thanks!
Was This Post Helpful? 0
  • +
  • -

#5 tuxandpucks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 12

Re: Content Formatting Help Needed.

Posted 03 June 2012 - 05:48 PM

View Poste_i_pi, on 03 June 2012 - 05:00 PM, said:

This will be more of a case of what the format of the data is in the database, whether is has line-breaks and carriage returns, or the poem is formatted in some other way.

Can you give an example of data from the DB, using a query like this:
SELECT *
FROM poems
LIMIT 3


That should get 3 poems from the DB, probably the first three that were entered.



I have attached to images. Image one shows the result of the query you suggested.
The second image is what my actual poem looks like in the database.

Attached Image
Attached Image

thanks,
Randy
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Content Formatting Help Needed.

Posted 03 June 2012 - 06:02 PM

Without seeing how your table is configured, I'll guess that something like this should work:
echo '<div class="mypoem">';
while($row=mysql_fetch_row($result))
{
	echo '<span class="title">Title:</span>'.$row["title"];
	echo '<span class="poem">Poem:</span>'.$row["Poem"];
}
echo '</div>';


Now you only need to use the appropriate css to set the format, and then you can get on with the more important issue.
Was This Post Helpful? 0
  • +
  • -

#7 tuxandpucks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 12

Re: Content Formatting Help Needed.

Posted 03 June 2012 - 06:11 PM

View PostCTphpnwb, on 03 June 2012 - 06:02 PM, said:

Without seeing how your table is configured, I'll guess that something like this should work:
echo '<div class="mypoem">';
while($row=mysql_fetch_row($result))
{
	echo '<span class="title">Title:</span>'.$row["title"];
	echo '<span class="poem">Poem:</span>'.$row["Poem"];
}
echo '</div>';


Now you only need to use the appropriate css to set the format, and then you can get on with the more important issue.


Thank you, CTphpnwb!

I would link to my css file in the head tags as I would a html file, correct?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Content Formatting Help Needed.

Posted 03 June 2012 - 06:18 PM

It is HTML that's being formatted. PHP runs on the server and is never seen by the browser, so it is not formatted by it! The browser only sees HTML that is ignored by the php interpreter or output by it.
Was This Post Helpful? 0
  • +
  • -

#9 tuxandpucks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 12

Re: Content Formatting Help Needed.

Posted 04 June 2012 - 03:26 PM

No luck with your most recent code, CTphpnwb. I am certain something is wrong on my end. Is there any other information I can provide to help you help me?


thanks,
Randy
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Content Formatting Help Needed.

Posted 04 June 2012 - 05:52 PM

You might try mysql_fetch_assoc() but remember that we're assuming your table has fields called title and Poem that contain the appropriate data. Of course, I would use PDO so the code would be different — and secure.

Also, this is not a good way do describe a problem:

Quote

No luck with your most recent code

You're missing:
  • Relevant code as you implemented it.
  • Error message(s).
  • Output

Was This Post Helpful? 0
  • +
  • -

#11 tuxandpucks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 12

Re: Content Formatting Help Needed.

Posted 05 June 2012 - 04:52 AM

View PostCTphpnwb, on 04 June 2012 - 05:52 PM, said:

You might try mysql_fetch_assoc() but remember that we're assuming your table has fields called title and Poem that contain the appropriate data. Of course, I would use PDO so the code would be different — and secure.

Also, this is not a good way do describe a problem:

Quote

No luck with your most recent code

You're missing:
  • Relevant code as you implemented it.
  • Error message(s).
  • Output



I will try to implement mysql_fetch_assoc(). Thanks for the tip.
Also, I believe post number five shows my table fields.
thank you and I apologize for the lack of information in my last response.
Although I have heard of it and read a bit about it, I haven't learned enough of the "foundation" of PHP to venture to PDO - I don't think I have, anyway.
Was This Post Helpful? 0
  • +
  • -

#12 tuxandpucks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 12

Re: Content Formatting Help Needed.

Posted 05 June 2012 - 05:24 AM

UPDATE:

I managed to accomplish what I wanted (for now). I did so with the following code:

$result=mysql_query("SELECT * FROM poems WHERE title = '".$_GET["title"]."'") or die("Could not connect to database!");



while ($row = mysql_fetch_array($result)) {
	print "<p><h3>{$row['title']}</h3>
	              {$row['poem']}<br />
	       </p><hr />\n";
}



And, then in my database I just added <pre></pre> tags around each poem.

Question: Is that a bad idea/bad practice to add the <pre> tags to my data in the database?

Thanks,
Randy
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Content Formatting Help Needed.

Posted 05 June 2012 - 07:47 AM

I would put it in the output so you won't have to remove it later when you want to do something different.

Attention: You're making an important (and frequent) beginner mistake in that you're trying to get your code to work when you should be trying to write it correctly, in a well organized and structured manner. You should be thinking about using functions and avoiding SQL (see post #3) injection. Concentrate on writing organized code and it will be easy to make it work. Concentrate on making it work and you'll end up with a disorganized mess that's difficult to edit or debug.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1