473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem creating working array value pair

I am working on some scripts to help me automate the website creation
process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

I actually have a working system, but I thought I would improve by
using comma-separated key => value pairs.

The code is below.

I am using the part of the page filename as the key and the value as
the <title></title>

Problem is that the first 9 work, that is I get the correct page
linked.

When I enter
'http://localhost/template.php?page=plan2', I get the default 'home'
page.

For the life of me I don't see what is wrong.
I use HTML-Kit and notepad. (in the process or weening of wysiwyg
products)

I considered it was a text encoding problem but the same whether I use
notepad or HTML-Kit.
<?php

$allpages=array(
'Home Page'=>'home',
'Purpose'=>'purpose',
'Spiritual Gifts'=>'spiritgifts',
'Reaching Potential'=>'potential',
'Weight Loss'=>'weightloss',
'Contact Me'=>'contactme',
'Spiritual Gifts2'=>'gifts2',
'Spiritual Gifts3'=>'gifts3',
'Links'=>'links',
'The Plan'=>'plan1',
'The Plan'=>'plan2',
'The Plan'=>'plan3'
);
if (isset($_GET['page']) AND (array_search($_GET['page'], $allpages)))
{ $thispage=$_GET['page'];
$title=array_search($thispage, $allpages);
}
else
{ $thispage='home';
$title="Home Page";
}

?>

Jun 14 '06 #1
10 1804
alanbe wrote:
I am working on some scripts to help me automate the website creation
process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

Beware that search engines such as google will not follow thes links and
thus the search engine ranking of the site will suffer.
Jun 14 '06 #2
alanbe wrote:
I am working on some scripts to help me automate the website creation
process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

I actually have a working system, but I thought I would improve by
using comma-separated key => value pairs.

The code is below.

I am using the part of the page filename as the key and the value as
the <title></title>

Problem is that the first 9 work, that is I get the correct page
linked.

When I enter
'http://localhost/template.php?page=plan2', I get the default 'home'
page.

For the life of me I don't see what is wrong.
I use HTML-Kit and notepad. (in the process or weening of wysiwyg
products)

I considered it was a text encoding problem but the same whether I use
notepad or HTML-Kit.
<?php

$allpages=array(
'Home Page'=>'home',
'Purpose'=>'purpose',
'Spiritual Gifts'=>'spiritgifts',
'Reaching Potential'=>'potential',
'Weight Loss'=>'weightloss',
'Contact Me'=>'contactme',
'Spiritual Gifts2'=>'gifts2',
'Spiritual Gifts3'=>'gifts3',
'Links'=>'links',
'The Plan'=>'plan1',
'The Plan'=>'plan2',
'The Plan'=>'plan3'
);
if (isset($_GET['page']) AND (array_search($_GET['page'], $allpages)))
{ $thispage=$_GET['page'];
$title=array_search($thispage, $allpages);
}
else
{ $thispage='home';
$title="Home Page";
}


Further on the actual problem, it seems that if a key value is repeated,
then array_search will only return that key if the value found is the last
one in the list with the same keys. So for your example, it'll work for
'plan3' but not for 'plan2'. Likewise, is the keys for Spiritual Gifts were
both 'Spiritual Gifts' then it would work for 'gifts3' but not for 'gifts2'.

Why not make use of the associative properties of the array and swap all the
keys and values thus:

$allpages = array (
'home' => 'Home Page',
'purpose' => 'Purpose',
'spiritgifts' => 'Spiritual Gifts',
'potential' => 'Reaching Potential',
'weightloss' => 'Weight Loss',
'contactme' => 'Contact Me',
'gifts2' => 'Spiritual Gifts2',
'gifts3' => 'Spiritual Gifts3',
'links' => 'Links',
'plan1' => 'The Plan',
'plan2' => 'The Plan',
'plan3' => 'The Plan'
);

$thispage=$_GET['page'];

if (!$title = $allpages[$thispage])
{
$title = $allpages[$thispage = 'home'];
}
Jun 14 '06 #3
Rik
Paul Lautman wrote:
alanbe wrote:
I am working on some scripts to help me automate the website creation
process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

Beware that search engines such as google will not follow thes links
and thus the search engine ranking of the site will suffer.


That was ages ago.
As long as he is using a real domain instead of localhost they will de
indexed fine. Maybe they'll give a light penalty, but nothing really
serious.

Grtz,
--
Rik Wasmus
Jun 14 '06 #4
Rik
alanbe wrote:
Problem is that the first 9 work, that is I get the correct page
linked. if (isset($_GET['page']) AND (array_search($_GET['page'],
$allpages))) { $thispage=$_GET['page'];
$title=array_search($thispage, $allpages);
}


try:
if (isset($_GET['page']) && in_array($_GET['page'],$allpages)){
//code
}

array_search returns a key, not a boolean. Might work in a lot of cases, but
highly quircky here.
Like it states in the maual:

Warning:
This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing the return
value of this function.

I would have reversed the array though:
'plan1' => 'The Plan' etc..

if(isset($_GET['page']) && array_key_exists($_GET['page'],$allpages)){
$thispage = $_GET['page'];
$title = $allpages[$_GET['page']];
}

Grtz,
--
Rik Wasmus
Jun 14 '06 #5
Rik wrote:
Paul Lautman wrote:
alanbe wrote:
I am working on some scripts to help me automate the website
creation process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

Beware that search engines such as google will not follow thes links
and thus the search engine ranking of the site will suffer.


That was ages ago.
As long as he is using a real domain instead of localhost they will de
indexed fine. Maybe they'll give a light penalty, but nothing really
serious.

Grtz,


Google still says that they do not look at them

"Don't use "&id=" as a parameter in your URLs, as we don't include these
pages in our index. "
Jun 14 '06 #6

Paul Lautman wrote:
'http://localhost/template.php?page=links'

Beware that search engines such as google will not follow thes links and
thus the search engine ranking of the site will suffer.


On my host, if I call http://server/bla/links, and there is no bla
directory, bla.php is executed. This makes it possible to have nicer
URLs, all served by one page. I think this is called MultiViews.

Jun 14 '06 #7
Paul Lautman wrote:
When I enter
'http://localhost/template.php?page=plan2', I get the default 'home'
page.

For the life of me I don't see what is wrong.
<?php
$allpages=array( .... 'The Plan'=>'plan1',
'The Plan'=>'plan2',
'The Plan'=>'plan3'
);


Further on the actual problem, it seems that if a key value is repeated,
then array_search will only return that key if the value found is the last
one in the list with the same keys. So for your example, it'll work for


Without intending bearing on the OP's problem ... If you want to search
for multiple instances of the same value in an array, you can use
array_intersect to return all the matching instances, complete with
original keys:
$aRes = array_intersect($allpages, array("search term"));
So if you wanted to get the last matching key you could do:
$lastKey = array_pop(array_keys(array_intersect($allpages,
array("search term"))));

Csaba Gabor from Vienna

Jun 14 '06 #8
Here is my method.
<?php
$allpages = array(
'home' => 'Home Page',
'purpose' => 'Purpose',
'spiritgifts' => 'Spiritual Gifts',
'potential' => 'Reaching Potential',
'weightloss' => 'Weight Loss',
'gifts2' => 'Spiritual Gifts 2',
'gifts3' => 'Spiritual Gifts 3',
'links' => 'Links',
'plan' => 'The Plan',
'plan2' => 'The Plan',
'plan3' => 'The Plan'
);
// Make an associative array by use its key as page name.

if (!isset($allpages[$thispage = $_GET['page']]))
$thispage = 'home';
// If the requested page does not exists, just show the home!

$title = $allpages[$thispage];
// Get its title.

echo "<h1>$title</h1>";
// Display its title to test.
?>

Jun 14 '06 #9
Rik
Paul Lautman wrote:
Rik wrote:
Paul Lautman wrote:
alanbe wrote:
I am working on some scripts to help me automate the website
creation process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'
Beware that search engines such as google will not follow thes links
and thus the search engine ranking of the site will suffer.


That was ages ago.
As long as he is using a real domain instead of localhost they will
de indexed fine. Maybe they'll give a light penalty, but nothing
really serious.

Grtz,


Google still says that they do not look at them

"Don't use "&id=" as a parameter in your URLs, as we don't include
these pages in our index. "


google recognizes a lot in the query string.
id=, PHPSESSID= etc. are indeed not indexed.

&page=something will

Grtz,
--
Rik Wasmus
Jun 14 '06 #10

DtTvB

I tried this and it works!
Thanks alot DtTvB and everyone else who replied..

alanbe

the DtTvB wrote:
Here is my method.
<?php
$allpages = array(
'home' => 'Home Page',
'purpose' => 'Purpose',
'spiritgifts' => 'Spiritual Gifts',
'potential' => 'Reaching Potential',
'weightloss' => 'Weight Loss',
'gifts2' => 'Spiritual Gifts 2',
'gifts3' => 'Spiritual Gifts 3',
'links' => 'Links',
'plan' => 'The Plan',
'plan2' => 'The Plan',
'plan3' => 'The Plan'
);
// Make an associative array by use its key as page name.

if (!isset($allpages[$thispage = $_GET['page']]))
$thispage = 'home';
// If the requested page does not exists, just show the home!

$title = $allpages[$thispage];
// Get its title.

echo "<h1>$title</h1>";
// Display its title to test.
?>


Jun 14 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Glutinous | last post by:
I've been studying this for hours, searching the www & usenet, and still can't figure out why 'each' returns an array of four key/value pairs, when it looks like just two pairs would suffice... ...
6
by: Fnark! | last post by:
I am creating a shopping cart using PHP Version 4.1.2. I am creating and registering a cart object in a session. The cart object contains an array of arrays called $order whose elements are a...
4
by: PhilC | last post by:
Hi Folks, If I have an array holding a pair of numbers, and that pairing is unique, is there a way that I can find the array index number for that pair? Thanks, PhilC
2
by: Zenobia | last post by:
I have a problem. I need to look up several values stored in arrays. Each value is stored as a pair. The first (number) represents the probability of this item occurring. For instance, in the...
3
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C or C++ code...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
4
by: skavan | last post by:
Use Case: We have music files that describe, in their filename, attributes of the music. We do not know a general pattern that applies to all filenames -- but we do know that filenames that are...
13
by: lane straatman | last post by:
I'm trying to figure out what data type is appropriate to represent a card in a game. The idea that I thought was going to work was a struct, foo, with two integer fields and two fields of char...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.