Page 1 of 1

Combining two PHP Functions

Posted: Sat Jun 30, 2007 1:55 pm
by smoogle
I currently have two php functions with the parameters $id, $title, $url, and $description. The parameters $id and $title are common to both function and ideally would be the same values in each function.

Code: Select all

<?php function myToolbarButtons($id,$title,$url,$description) { ?>
		<a id="<?=$id?>" href="<?=$url?>" title="<?=$description?>"><?=$title?></a>
	<?php } ?>

	<?php function myToolbarContent($id,$title) { ?>
		<div id="<?=$id?>-section">
			<h2><?=$title?></h2>
		</div>
	<?php } ?>
These two functions I call in the body of my web page like so:

Code: Select all

<div id="buttons">

    	<?php myToolbarButtons(about,About,http://www.asite.com/about/index.php,About this site);?>
    	<?php myToolbarButtons(contact,Contact,http://www.asite.com/contact/index.php,Drop us a line);?>
    	<?php myToolbarButtons(comment,Comment,http://www.asite.com/comment/index.php,Voice your opinion);?>

    </div>

and.....

<div>
    
	<?php myToolbar(about,About);?>
	<?php myToolbar(contact,Contact);?>
	<?php myToolbar(about,Contact);?>

</div>
What I would like to do is somehow combine both functions together so I could make one call and insert my parameters and it would output the html in the same format as above, i.e:

Code: Select all

<?php myNewToolbar(about,About,http://www.asite.com/about/index.php,About this site);?>
<?php myNewToolbar(contact,Contact,http://www.asite.com/contact/index.php,Drop us a line);?>
I would very much appreciate it if anyone could offer their assistance...maybe it's a bit of a newbie question but it would really help me to know how I could achieve this.

Posted: Sat Jun 30, 2007 2:12 pm
by alex.barylski
I'm confused. What is it your trying to accomplish?

It seems as though you output your toolbar buttons and at the same time output each content page as well? Don't you need some conditionals to test whether the body for toolbar button "A" should be displayed, but others not?

I would suggest using a template engine (Google: bTemplate) so you can use a single function and not repeat setting title more than once.

Posted: Sat Jun 30, 2007 4:25 pm
by smoogle
Hi and thank you for your reply. I must admit that the example I have included has much of the code stripped away so that it would be a little simpler to try to address what I am trying to achieve. Basically I have a toolbar which maps links to urls, when the links are clicked on it loads the url using ajax and populates a div by its id, hence the id-section div id.

I don't know that what I am asking is feasible but I am trying to make the code as simple and compact as possible so instead of having to call two php function and insert almost the same parameters into each, I want to call one function, insert the parameters but the output displays the toolbar (with all toolbar links) followed by the content placeholders for each specific link within the toolbar.

Code: Select all

<div id="toolbar">
link 1
link 2
link 3
</div>

<div
placeholder 1
placeholder 2
placeholder 3
</div>
I hope this is a little clearer but I fear I may have probably confused you more.

Posted: Sat Jun 30, 2007 4:34 pm
by smoogle
Here is the actual code: First the two functions followed by the two PHP calls:

Code: Select all

<!-- PHP Function for drawing out image link icon -->

	<?php function mToolbarButtons($id,$title,$description) { ?>
		<a id="<?=$id?>-toggle" href="#" name="<?=$id?>-toggle class="tooltip" title="<?=$title?> :: <?=$description?>"><img src="<?=$imagePath?>icon-m-<?=$id?>.png alt="" /></a>
	<?php } ?>

	<!-- PHP function for drawing out content to be effected -->

	<?php function mToolbarContent($id,$title) { ?>
		<div id="<?=$id?>-section" class="hide">
			<div class="section-content">
				<h2><span class="right"><a id="<?=$id?>-slideout" href="#" name="<?=$id?>-slideout"><img src="<?=$imagePath?>icon-s-close.png" alt="close" /></a></span><?=$title?></h2>
				<div class="section-inner">
					<div id="<?=$id?>Content">
					</div>
				</div>
			</div>
		</div>
	<?php } ?>
	
</head>

<body>

<div id="toolbar">
	<div id="buttons">
    	<?php mToolbarButtons(about,About,About this site);?>
    	<?php mToolbarButtons(contact,Contact,Drop us a line);?>
    	<?php mToolbarButtons(comment,Comment,Voice your opinion);?>
    </div>
</div>
    
	<?php mToolbarContent(about,About);?>
	<?php mToolbarContent(contact,Contact);?>
	<?php mToolbarContent(comment,Comment);?>

</body>

Posted: Sat Jun 30, 2007 6:38 pm
by superdezign
Firstly, always enclose strings in quotation marks.

Secondly, what you want to do could be easily done with object-oriented programming, but I think what you'll want to do is send all of the parameters as arrays.

Code: Select all

function AllButtonsOrWhatever($data)
{
    echo '<div id="toolbar"><div id="buttons"> ';

    foreach($data as $item)
    {
        echo '<a id="'.$item['id'].'-toggle" href="#" name="'.$item['id'].'-toggle class="tooltip" title="'.$item['title'].' :: '.$item['description'].'"><img src="'.$item['imagePath'].'icon-m-'.$item['id'].'.png alt="" /></a>';
    }

    echo '</div></div>';

    foreach($data as $item)
    {
        // echo the other part
    }
}

AllButtonOrWhatever(array(array('id' => 'page1', 'title' => 'Page #1', ... )));
You'd put all of your data into nested arrays and run it through.