TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-27-2007, 10:24 PM   #1 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Wales - UK
Posts: 8
Thanks: 0
Dorza is on a distinguished road
Default Extracting page titles and placing into dropdown box.

Hi, I've been trying to figure this out for the past few days but can't do it. I am usless at explaining things so I'll do it in point form..

p1 ). I have a text area where an article is written e.g.

Code:
<h5>Title for Page 1</h5>
Page one content

<h5>Title for Page 2</h5>
Page two content

<h5>Title for Page 3</h5>
Page three content
This then gets submitted to the db where it is retreived when a viewer wants to read the article...

p2) On the public front end a script splits the record up into 3 pages and outputs links such as "Prev - 1, 2 ,3 - Next">. I have reached this stage and it all works perfectly.

p3) The issue(s*) I have is that to make navigating easier I would like the page titles to appear in a drop down box and when submitted the user would be taken to the appropriate page; See here.

How does one extract the page titles and stick them in a drop down box?

*In addition to this I am using [NEWPAGE] above each page title (<h5> tags) in the article creation text box to indicate a new page and its this that explode() looks for when creating pages, I would rather it actually search for the page titles them selves which are housed between <h5> tags.

Last edited by Dorza : 10-28-2007 at 12:02 AM.
Dorza is offline  
Reply With Quote
Old 10-27-2007, 10:25 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

If the title is stored in the database, you could quite simply run a query and return all the titles of all the pages.
Tanax is offline  
Reply With Quote
Old 10-28-2007, 12:01 AM   #3 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Wales - UK
Posts: 8
Thanks: 0
Dorza is on a distinguished road
Default

Thanks for your suggestion, it gave me an idea. It's weird how one can spend days trying to figure something out and they think they have exhausted all options that their skill will allow for, but then you read a suggestion on the net, which gives you an idea which is simplistic in nature to implement and then you think of your self as a muppet for not being able to think the solution up previously....

PHP Code:
$pNum 1;
$arrayNum 0;
$pageUrl "http://$RootUrl/some_dir/some_scirpt.php?ArticleId=$aId&page=";

//BELOW: $body is the relevant content from the db
$explode explode("[NEWPAGE]"$body);
$pageCount count($explode) -1;

preg_match_all("/<h5>(.*)<\/h5>/U"$body$fnd_titles);
$numTitles count($fnd_titles[1]) - 1;

// echo form tag

echo "<select name=\"pageNav\"> \n";

while(
$arrayNum <= $numTitles)
{
   while(
$pNum <= $pageCount)
   {
     echo 
"<option value=\"$pageUrl"."$pNum\">".$fnd_titles[1][$arrayNum]."</option>\n";
     
$pNum++;
     
$arrayNum++;
    }
}

echo 
"</select>\n";

//echo form submit button
//echo form close tag 
Not sure if the above works after tweaking things a bit after moving it from my editor, but on the site it works; needs some refining but I'm pointed in the right direction with it. As far as my code is concerned how should it be tided up and streamlined? I also still have that issue with [NEWPAGE] and wanting to use <h5></h5> instead.
Dorza is offline  
Reply With Quote
Old 10-28-2007, 02:39 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Where is your page content coming from with the above code?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-28-2007, 02:51 PM   #5 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Wales - UK
Posts: 8
Thanks: 0
Dorza is on a distinguished road
Default

I've trimmed all the layout elements out as well as any non essential php...

PHP Code:
$getNews mysql_query(............);
while(
$news mysql_fetch_array($getNews))
{
    
$body $news['article_body']); 


    echo 
$body;
    
//echo pagination here
   //echo my previous code listing here

Is this what you wanted? The entire script is around 120 lines long which also includes a pagination script and validation features for it.
Dorza is offline  
Reply With Quote
Old 10-28-2007, 07:07 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Here's something which might be useful, please don't just use the code as-is but take it's ideas and concepts (and main function hehe) to fit into your project. The code provided will (should) work in a standalone manner just to show you everything. It uses preg_split, I'm sure there are other alternatives of which this is just one example.

PHP Code:
<?php

/**
 * Splits a document by <h5>...</h5> elements
 *
 * This function does all of the work for you, hopefully.
 * Simply feed it an article and it will spit out 
 * the pages in an array containing title and body items.
 */
function split_pages($szArticle$szTag 'h5')
{
    
$aPages    = array();
    
$szTag     preg_quote($szTag);
    
$szPattern '#^\s*<' $szTag '>(.*?)</' $szTag '>\s*#im';
    
$aItems    preg_split($szPattern$szArticle, -1PREG_SPLIT_DELIM_CAPTURE);
    
array_shift($aItems);
    
    
// Loop through and sort into title/body groups
    
for ($i 0$l count($aItems); $i $l$i $i+2)
    {
        if (!isset(
$aItems[$i+1]))
            continue;
            
        
$aPages[] = array('title' => trim($aItems[$i]),
                          
'body'  => trim($aItems[$i+1]));
    }
    
    return 
$aPages;
}

// Here is our article to use 
$szArticle '<h5>Title for Page 1</h5>
Page one content

<h5>Title for Page 2</h5>
Page two content

<h5>Title for Page 3</h5>
Page three content'
;

// Gather the necessary variables together
$aPages split_pages($szArticle);
$iPage  = (isset($_GET['page']) && array_key_exists(intval($_GET['page']) - 1array_keys($aPages))) ? $_GET['page'] : 1;
$aPage  $aPages[$iPage 1];


/*
    Now onto some examples of how you might 
    like to use the pages array.
*/

?>

<!-- Show us what the $aPages array looks like -->
<pre><?php print_r($aPages?></pre>
<hr>

<!-- Show Current Page -->
<h2>Page <?php echo $iPage ?><?php echo $aPage['title'?></h2>
<p><?php echo nl2br($aPage['body']) ?></p>
<hr>

<!-- Pagination, Navigation, whatever you want to call it -->
<ol>
<?php foreach ($aPages as $t_iPage => $t_aPage) : $t_iPage++; ?>
    <li>
        <a href="?page=<?php echo $t_iPage ?>">
            Page <?php echo $t_iPage ?><?php echo $t_aPage['title'?>
        </a>
    </li>
<?php endforeach; ?>
</ol>
Salathe is offline  
Reply With Quote
Old 10-29-2007, 11:01 PM   #7 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Wales - UK
Posts: 8
Thanks: 0
Dorza is on a distinguished road
Default

That was very generous of you to do that salathe. I got it working, after some tinkering. I'm now working on adding the prev - next links back in. I will post the finalized script in the coming days. Thanks again for that script it works a charm.

Oh when I say it works a charm, there is one slight problem. It appears that if there is a string before any <h5> tag then the script won't split the content into a new page for some reason, so all <h5> tags must be on a new line. For example the below string produces just two pages when really it should be three...
Code:
$azArticle = "<h5>page 1</h5>page one content
              <h5>page 2</h5>page two content<h5>page 3</h5>page three content";
Yet this example would produce the desired three pages...
Code:
$azArticle = "<h5>page 1</h5>page one content
              <h5>page 2</h5>page two content
              <h5>page 3</h5>page three content";
I changed $azArticle to = $cont['cont_body'] which comes from my db and it refused to split content into more than one page and instead put all the pages into page 1. Its because of the reasoning above. But due to the way my text editor (tinyMCE) places data into the db there is no practical way from me to sort it out.
Dorza is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:09 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design