Uutiset:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu
Advertisement:

need help with sub actions in my custom page

Aloittaja leroymcqy, toukokuu 14, 2014, 10:57:29 IP

« edellinen - seuraava »

leroymcqy

i am trying to get it where i can use sub actions for pages but it isnt working right.


<?php
if (!defined('SMF'))
die('Hacking attempt...');

function 
about()
{

// Second, give ourselves access to all the global variables we will need for this action
global $context$scripturl$txt$smcFunc;

// Third, Load the specialty template for this action.
loadTemplate('About');

//Fourth, Come up with a page title for the main page
$context['page_title'] = 'About';
$context['page_title_html_safe'] = $smcFunc['htmlspecialchars'](un_htmlspecialchars($context['page_title']));

 
$context['linktree'][] = array( // The linktree which is displayed above the content
      
'url' => $scripturl '?action=about'// Link to the highscores page
      
'name' => 'About',
   );
   
$subActions = array( // subActions will be described soon
      
'History of Suicide' => 'history'
   
);

      
show_about();

              
 if (!empty(
$subActions[@$_GET['page']]))
 
      
$subActions[$_GET['page']]();
   else
     
show_about();

}

function 
show_about() {
   global 
$context;
   
   
$username "smf875";
$password "8SPhh1!U.6";
$hostname "localhost"

//connection to the database
$dbhandle mysql_connect($hostname$username$password)
  or die(
"Unable to connect to MySQL");


//select a database to work with
$selected mysql_select_db("smf875",$dbhandle)
  or die(
"Could not select $selected");
  
  
$result mysql_query("SELECT * FROM about");


while (
$row mysql_fetch_array($result)) {
   
$body=$row['body'];
   
$title=$row['title'];
   
$context['message'] = "
   <h2>
$title</h2>
   
$body";
   
}
  
}

function 
history()
{

loadTemplate('history');





//select a database to work with
$selected mysql_select_db("smf875",$dbhandle)
  or die(
"Could not select $selected");
  
  
$result mysql_query("SELECT * FROM sadmin_history");


while (
$row mysql_fetch_array($result)) {
   
$body=$row['content'];
   
$title=$row['body'];
   
   echo 
'<div class="cat_bar">
    <h3 class="catbg">'
.$title.'</h3>
</div>'
;
  
  
// Windowbg2 Content
echo '<div class="windowbg2">
   <span class="topslice"><span></span></span>
      <div class="content">'
$body'</div>
   <span class="botslice"><span></span></span>
</div><br />'
;
   
}
}

?>




i can't remember how to get it to work properly if some one could show me what do i place to make it work and where at. template file shows same thing even when i go to the history subaction.

Arantor

Wait, you're trying to have index.php?action=about;page=History of Suicide as a URL? That really isn't going to work out too well for you.

In any case you need to remove the first call to show_about() before the test against $subActions is carried out.
Holder of controversial views, all of which my own.


leroymcqy

i fixed that part and removed the showabout before subpage call and then realized i had to make a template for the subaction.

if there is a way to show the subaction without a template file for it self. (called in same template file) it would be nice. but i got it to show now

Arantor

Well, you only need one template for the entire action, that's easy enough. In the subaction, just override $context['sub_template'] to point to whichever subtemplate should be shown. If you want to use a function called template_history() in your template file, you can set $context['sub_template'] to 'history' and it'll work as expected.

The best guide, as ever, is SMF's own code because that's how SMF does it.

Also, it is extremely poor form to do the database stuff yourself in an action unless for some insane reason you're fetching it from outside SMF's database. The connection's already in place, there's dedicated functions for querying the database which are also able to be future proofed, using SELECT * is almost never a good idea, and using mysql_fetch_array is NEVER a good idea.
Holder of controversial views, all of which my own.


Advertisement: