Forum Moderators: coopster

Message Too Old, No Replies

Simple php help

         

ads112001

2:06 pm on May 13, 2011 (gmt 0)

10+ Year Member



So i'm working on my portfolio site and i'm new at writing php. I'd like to have an array which gets pulled in and displayed in a couple of different ways (1 as a large slide show and then categorized thumb gallery).

In my php page, i have
display_portfolio('main');
and
display_portfolio('websites');
but both are displaying the
else
code.

I'm sure there's a better way to do this but i'm still a newb so any help is greatly appreciated


<?php

function display_portfolio($category)
{
$category = array('main,websites,identity,graphics,misc');

$mainMenu = array(
"1" => array (
"title" => 'Mohegan Sun Meetings',
"description" => 'Mohegan Sun Meetings Redesign',
"category" => 'Web',
"url" => "img/portfolio/meetings1.jpg"
),
"2" => array (
"title" => 'Sun WineFest',
"description" => 'Sun WineFest Redesign',
"category" => 'Web',
"url" => "img/portfolio/winefest1.jpg"
),
"3" => array (
"title" => 'Mohegan Sun Website',
"description" => 'Mohegan Sun Website',
"category" => 'Web',
"url" => "img/portfolio/moheganSun1.jpg"
),
"4" => array (
"title" => 'What\s On Tap Logo',
"description" => 'What\s On Tap Logo',
"category" => 'Identity',
"url" => "img/portfolio/whatsOnTap-logo.jpg"
),
"5" => array (
"title" => 'i Personal Training &amp; Nutritional Services Website and Identity',
"description" => 'i Personal Training &amp; Nutritional Services Website and Identity',
"category" => 'Identity' && 'Web',
"url" => "img/portfolio/ihealth1.jpg"
),
);

for ($i = 1; $i <= count($mainMenu); $i++)
{
if ($category == "main") {
echo '<a href="' . $mainMenu[$i]['url'] .'" rel="prettyPhoto[' . $mainMenu[$i]['title'] .']"><img src="' . $mainMenu[$i]['url'] .'" alt="' . $mainMenu[$i]['title'] .'" title="' . $mainMenu[$i]['description'] .'" /></a>';
} else {
echo '<li><a href="' . $mainMenu[$i]['url'] .'" rel="prettyPhoto[' . $mainMenu[$i]['title'] .']"><img src="' . $mainMenu[$i]['url'] .'" alt="' . $mainMenu[$i]['title'] .'" title="' . $mainMenu[$i]['description'] .'" /></a></li>';
}
};
}
?>

rocknbil

5:21 pm on May 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not clear why you have a hard-coded array inside a function, or why you're echoing it within the function instead of using a return value, but at any rate, $category is being overwritten by an array. Look . . . a function is generally used like so . . .


function display_portfolio($category){
return "some value and category is $category";
}


This function accepts some parameter, names it $category, and returns a value of some sort, a scalar (string,) array, or object. So it's usage should be something like

$out = display_portfolio('main');
echo $out;

so it should echo

some value and category is main

But at the top of the function you have

$category = array('main,websites,identity,graphics,misc');

... which successfully replaces any parameters you are passing to the function. $category is now an array. So when you do this,

if ($category == "main") {

it doesn't know what you're thinking. :-)


As I look over your function I don't even see where that array is being used. Remove it or comment it out.

ads112001

5:56 pm on May 13, 2011 (gmt 0)

10+ Year Member



Thanks for the reply. Perhaps there is a better way to do what i'm trying to do. All I want is to enter my portfolio pieces in 1 spot (the name of the piece, the url for an img...) and have it pulled into 2 or 3 different sections. Sometimes displayed as an unordered list and sometimes not.