Jump to content

multiple record array


cactus

Recommended Posts

Hi,

 

I have a menu that is generated from my mysql database and I want it on every page of my website as the main menu. However I can''t figure out how to do this. I managed to get it on the view article page by duplicating the my query like this:

function viewArticle() {

  if ( !isset($_GET["articleId"]) || !$_GET["articleId"] ) {
    homepage();
    return;
  }

  $result['article'] = Article::getById( (int)$_GET["articleId"] ); //single record
  $result['pageTitle'] = $result['article']->title . " | Widget News";

  $results = array(); 
  $all_data = Article::getList( HOMEPAGE_NUM_ARTICLES ); //multiple record
  $results['articles'] = $all_data['results'];
  $results['totalRows'] = $all_data['totalRows'];

  require( TEMPLATE_PATH . "/viewArticle.php" );
}

 

However I can't figure out how to place it on for example the new article page.

function newArticle() {

  $results = array();
  $results['pageTitle'] = "New Article";
  $results['formAction'] = "newArticle";

  if ( isset( $_POST['saveChanges'] ) ) {

    // User has posted the article edit form: save the new article
    $article = new Article;
    $article->storeFormValues( $_POST );
    $article->insert();
    header( "Location: admin.php?status=changesSaved" );

  } elseif ( isset( $_POST['cancel'] ) ) {

    // User has cancelled their edits: return to the article list
    header( "Location: admin.php" );
  } else {

    // User has not posted the article edit form yet: display the form
    $results['article'] = new Article;
    require( TEMPLATE_PATH . "/admin/editArticle.php" );
  }

}

 

Can anybody help me out i'm really struggling?

Thanks :)

Link to comment
Share on other sites

Put all of your functions that will be used across multiple scripts in an include file, something like 'functions.inc.php' and then include them at the start of every page.

 

include "functions.inc.php";

 

That way you won't have to duplicate code.

Link to comment
Share on other sites

I tried to add in the following code to another page

<div id="menu">

      <ul id="headlines">

<?php foreach ( $results['articles'] as $article ) { ?> //THIS LINE

        <li>
		<a href=".?action=viewArticle&articleId=<?php echo $article->id?>"><?php echo htmlspecialchars( $article->title )?></a>
        </li>

<?php } ?>

      </ul>
</div>

 

However I keep getting the following errors:

 

Undefined index: articles

 

and

 

Invalid argument supplied for foreach()

 

they appear on the line highlighted above can anybody help? Thanks :)

Link to comment
Share on other sites

how often does a menu change on a website? , is it really necessary to keep it in the database?, thats one extra db call on every page you dont really need

 

i use this as a common included file for a navigation on many sites i write/have written, obviously you would adapt the output to your needs:

$navarray['Logout'] = 'login.php?action=logout';
$navarray['View Exhibitions'] = 'exhibitions.php';
$navarray['My Sales'] = 'mysales.php';
$navarray['View Images'] = 'images.php';
$navarray['Consigned Images'] = 'consigned.php';
$navstring = "<div class=\"shadetabs\"><ul>";
foreach ($navarray as $key => $value)
    {
	$navstring .= "<li";

	if (strpos($_SERVER['PHP_SELF'], $value)) $navstring .= " class='selected'";
	$navstring .= ">\r";
	$navstring .= "<a href='" . $value  . "'>"; 
	$navstring .= $key;
	$navstring .= "</a>\r";					
	$navstring .= "</li>\r";
}
$navstring .= $outputstr;
$navstring .= "</ul></div>";
echo $navstring;

 

this out the menu into a <ul> list and those have so many css interpretations they can look like anything

Link to comment
Share on other sites

I am creating a cms to update the pages so the menu needs to be on each page to access the updated page, if that makes sense?

 

I'm really sorry but I don't understand what you mean with the post you have just sent i'm a beginner at php and mysql and am finding it difficult to figure out this problem. It's the last section of my website that I'm having problems with and just need the error messages fixed. So is there any way that I can fix these problems to get the menu working on each page?

 

I really hope someone can help. I'm really struggling, please :)

Link to comment
Share on other sites

hello,

 

sorry for constantly replying in my own post but I need to know how to get the menu that is generated from my database on every page. Is there even a way to do this?

 

or link the current menu icon image to one of the posts from the database?

 

Any ideas?? I can't figure it out.

 

Thanks in anticipation :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.