Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hey everyone,

 

I am attempting to create an array with my various poem titles that visitors to my website will be able to click on the poem title and read it.

 

My question is, how do I add the actual poem to this array (this is a short example of my code)?

 

So far I created the overall array for all the letters to sort with, A, B, C, etc., and the titles of each poem. Do I somehow add a link to the title to make that happen and have the link simply point to another html page? I'm starting to think that an actual poem with many lines doesn't fit into an array. Not sure what to do.

 

 

Code:

$poemsa = array ('All Over Town', 'Angel Upon A Breath',);

$poemsb = array ('The Battle of the Bulge');

$poemsc = array ('Choose', 'Coffee Anyone?', 'Coffee History', 'Combustion', 'Crash Test', );

$poemsd = array ('Double Meaning');

 

//Multidimensional Array

$poems = array(

'A' => $poemsa,

'B' => $poemsb,

'C' => $poemsc,

'D' => $poemsd,

 

So far I can successfully run these two bits of code:

Code:

print "<p>Poem:<i>{$poems['A'][0]}</i>.</p>";

 

 

foreach ($poems as $letter => $poem) {

echo "<p>$letter: $poem</p>\n";

}

 

Any ideas?

 

Thank you,

Randy

Link to post
Share on other sites

Hi Randy,

 

Welcome to the forums! Are you (or is there any reason you're not using) a database to store and retrieve the poems?

 

There's no material reason why you can store the poems in an array but storage in a database would make filtering, retrieving and on-going maintenance a lot easier!

 

Let us know if thats an option first.

 

Thanks

  • Upvote 1
Link to post
Share on other sites

I actually would love to use Mysql and my hosting provider has phpmyadmin already for me to use......i simply have never used Mysql and although i looked into it even using larry's php book i jist struggled woth it. If u could provide an example of how to enter my data i would certainly be willing to try agaon. O want to have my database table callwd poems, then letters a to z and just list my poems under each letter. Thank you for the help. Randy

Link to post
Share on other sites

Then this would be a good structure:

 

poems (id, title, category_id*)

- id (primary_key, auto_increment INT)

- title (varchar 255)

- category_id (INT 3) - Foreign key to table poems_category's category_id

 

poems_category (category_id, category_name)

- categeory_id (primary_key, auto_increment INT 3)

- category_name (varchar 255)

 

Do you need help generating the code for the structure? I have to go now. Sure someone can help you in the mean tine.

Link to post
Share on other sites

Then this would be a good structure:

 

poems (id, title, category_id*)

- id (primary_key, auto_increment INT)

- title (varchar 255)

- category_id (INT 3) - Foreign key to table poems_category's category_id

 

poems_category (category_id, category_name)

- categeory_id (primary_key, auto_increment INT 3)

- category_name (varchar 255)

 

Do you need help generating the code for the structure? I have to go now. Sure someone can help you in the mean tine.

 

 

Yes, I would need that help to generate the code. I will, in the meantime, see what I can do with creating a database with my web hosting providers tools.

 

Thank you, Randy

Link to post
Share on other sites

PhpMyAdmin has a really simple inmterface to use.

 

If you go into it you will first need to name your database.

 

 

Then from there name your table as Antonio has said `poems` and select 3 columns.

 

Then it will pop up with what to call your columns.

 

So 'id' will need a column type INT (for integer) this is available from a drop down, you also need to tick the auto_increment box and on the `index` section select primary key

 

name column 2 `title` and select the column type VARCHAR and the length as 255

 

column 3 - name `category_id` table type INT and length of 3

 

Press Save (i think) - It should be fine then the table should be created, PhpMyAdmin will then print out what the full query was, you can copy it, if you want to and paste it back to see that it worked ok. I really think you'll love using MySQL and PHPMyAdmin.

 

Let me know how you get on ;)

Link to post
Share on other sites

I really, truly appreciate the direct coaching you folks are giving me. Here is what phpmyadmin printed, when I selected "Print".

 

poems

 

Field Type Null Default Comments

id int(11) No

title varchar(255) No

category_id int(3) No

Indexes:

 

Keyname Type Unique Packed Field Cardinality Collation Null Comment

PRIMARY BTREE Yes No id 0 A

 

and this snippet was also showing after I created the db:

 

 

CREATE TABLE `tuxandp1_poems`.`poems` (

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,

`title` VARCHAR( 255 ) NOT NULL ,

`category_id` INT( 3 ) NOT NULL

) ENGINE = MYISAM ;

 

 

 

I guess my next step is to ask, How do I enter each of the letters of the alphabet so I may start entering poem titles? Or, is there something else to do first?

 

Thank you,

Randy

Link to post
Share on other sites

Well Looking at Antonio's post i would guess he suggested having a second table with category_id and category_name. I would therefore surmise that the idea was to use category_id = 1 where category_name = a, then category_id = 2 where category_name = b. (At a guess).

 

To start this process it is much like you did before. You do not need to make an new database, you can use your existing database and then add a new table called `whatever you want, maybe categories or something`.

 

add 2 columns for this new table

 

call one `category_id` and select it as an integer, tick auto increment as before, give it a length of 3 and select primary key

then the next column, `category_name` and select it as a VARCHAR, if you only want to store them alphabetically you only need a length of one.

 

That's it really. Then you need to go to the insert tab, and insert the individual letters into the db in the category_name field, the category_id should automatically insert itself. That table is then finished.

 

Just looking back at Antonio's structure, there doesn't seem to be a place to store the actual poem prose? Would you agree with this?

 

If so you need to select the tuxandp1_poems table and click the structure tab and it should say under the table layout, Add 1 field to... and some options like 'at the end of the table'.

 

Add 1 field to the end of the table. From there call the field 'poem' or something applicable. And set the table type as TEXT. Then save.

 

You can then add your poems text, poems title and the category it belongs to (i.e 1 if the poem falls under the 'A' category) by using the insert tab when you're viewing the poems table. The id should again increment on it's own. Shout me, if there's any problems

Link to post
Share on other sites

That's quite alright there are many people who can and will offer you help here, including Larry himself. MySQL opens up a whole new dimension to your programming, I don't know what is in the book you have as I don't have it, but if you enjoyed it and wanted to learn some more then there is a new PHP and MYSQL book coming out soon.

 

Anyway, good luck ;)

Link to post
Share on other sites

And yes, i forgot the actuall poem. :P

 

1. Click on the table poems in phpMyAdmin

2. Select "structure"

3. Look after "Add X column(s) below this structure

4. Select after "title" or what you called it.

5. Click add, call it "poem/text/whatever"

6. Select "Longtext" as type from the drop down list

 

One question: Is the letters only for sorting alphabetically? Then you could add the Letter directly into the poems table. You could also drop the whole categorization and select only queries that begins with the letter accoring to a variable etc. It depends on what you want.

 

With categories, you could sort the poems accoring to categories AND letters. It would give you some more choices. I dunno what your looking for.

 

This is how the data MAY look like:

poems(id, title, poem, category_id)

1 | my title........... | Poem Text | 1

2 | my best friend | Poem Text | 4

3 | my title........... | Poem Text | 2

4 | my title........... | Poem Text | 1

5 | my friend........ | Poem Text | 4

 

poems_categories (category_id, category_name)

1 | Dreams

2 | Nature

3 | Religious

4 | Frendship

5 | Whatever

Link to post
Share on other sites

And yes, i forgot the actuall poem. :P

 

1. Click on the table poems in phpMyAdmin

2. Select "structure"

3. Look after "Add X column(s) below this structure

4. Select after "title" or what you called it.

5. Click add, call it "poem/text/whatever"

6. Select "Longtext" as type from the drop down list

 

One question: Is the letters only for sorting alphabetically? Then you could add the Letter directly into the poems table. You could also drop the whole categorization and select only queries that begins with the letter accoring to a variable etc. It depends on what you want.

 

With categories, you could sort the poems accoring to categories AND letters. It would give you some more choices. I dunno what your looking for.

 

This is how the data MAY look like:

poems(id, title, poem, category_id)

1 | my title........... | Poem Text | 1

2 | my best friend | Poem Text | 4

3 | my title........... | Poem Text | 2

4 | my title........... | Poem Text | 1

5 | my friend........ | Poem Text | 4

 

poems_categories (category_id, category_name)

1 | Dreams

2 | Nature

3 | Religious

4 | Frendship

5 | Whatever

 

 

 

Yes, the letters are only for sorting/alphabetizing the poems.

 

If it is better to see a wireframe/mockup of my plan I can certainly post it.

 

Thanks again. I will give this a try (what you posted).

Link to post
Share on other sites

This is just thinking.

 

You add all letters into <button>-tags inside a form. This information will be used to query the correct poems.

 

<form action="" method="post">

<input type="submit" name="alphabetical" value="a" />
...
<input type="submit" name="alphabetical" value="z" />

</form>

 

You then use this information in your query:

 

// make sure data is good
$letter = $_POST['alphabetical'];

$query = "
  SELECT *
  FROM poems
  WHERE title LIKE '{$letter}%'
";

$result = mysqli_query($query, $connect);

// display info

 

Something like that. There are always several ways to solve a problem. :)

Link to post
Share on other sites

One tip. Instead of having a.php, b.php, etc. Make one general page called poems.php (or something) and base the query of poems on a $_POST og $_GET-variable. It would make more sense to develope ONE version than 26. :)

 

 

Tip will be used. Thanks.

 

Question about the MySQL setup. I click the "insert" tab on phpmyadmin to insert the poems, correct? I did this and it looks obvious as to that is what it is for, but i want to be sure.

 

thank you, randy

Link to post
Share on other sites

The question is how your structure are looking at the moment. Did you need categories, or do you only want to alphabetize? Are you planning on having several authors in the feature? Do you need to save the time published on the poems? These are questions you need to think about for the feature. It can always be altered later on, so I think we can start adding data.

 

---------------------------

Yes. Insert is the right place.

- Look after: "Continue insertion with X rows" in the bottom. Set it according to number of poems you want to add.

 

Enter data:

1. Leave id blank. This is automatic

2. Add title

3. Add poem content

4. The question is whether you need categories. Just add 1 or something here in the meantime.

 

Indeed that's what you do. Also I'm pretty sure:

 

$query = "SELECT * FROM poems WHERE substring(title, 1, 1) = '$letter'";

 

Is better practice/more efficient.

 

Than Mysql like function? I'm not 100% sure about that, but if you say so. :)

Link to post
Share on other sites

Well after conducting a quick test (I know its not been repeated, there are many confounding variables and ultimately means nothing) but using SUBSTRING to select all articles beginning with A from a 7000 row dataset took 0.0291 seconds in comparison to 0.3323 seconds using LIKE. I'm sure I could quite easily produce results to the contrary also, depending on the dataset etc... but to me if you want to extract a row that starts with a single character you should test specifically for that character.

 

That said if I was really worried about performance and using a huge dataset I'd probably add another column as a foreign key to represent the letters as then it could be indexed.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...