Jump to content

Recommended Posts

Hello all :)

I wanted to know of it is possible to create instances of two different classes in a single php page? Im trying to display a list of categories and another list of brands. But I am getting only the categories as result...

this is what i have done:

<?php $obj = new CategoryList();if (method_exists($obj, 'init')){	$obj->init(); }for($i = 0;$i< count($obj->mCategory); $i++)		{echo "<a href=''>";	echo $obj->mCategory[$i]['name']. "<br/>";	echo "</a>"; }   $obj2 = new BrandList();if (method_exists($obj2, 'init')){$obj2->init(); }for($i = 0;$i< count($obj2->mBrand); $i++){echo "<a href=''>";	echo $obj2->mBrand[$i]['name']. "<br/>";	echo "</a>"; } ?><?php

$obj = new CategoryList();
if (method_exists($obj, 'init'))
{
$obj->init();

}
for($i = 0;$i< count($obj->mCategory); $i++)		
{
echo "<a href=''>";	
echo $obj->mCategory[$i]['name']. "<br/>";	
echo "</a>";

}



$obj2 = new BrandList();
if (method_exists($obj2, 'init'))
{
$obj2->init();

}
for($i = 0;$i< count($obj2->mBrand); $i++)
{
echo "<a href=''>";	
echo $obj2->mBrand[$i]['name']. "<br/>";	
echo "</a>";

}

?>


 

Link to comment
https://forums.phpfreaks.com/topic/186684-instance-of-two-classes/
Share on other sites

Thanks for your reply! I think Ive put the code twice..

Can you please tell me why only the categories are being displayed?

Here's the code again:

<?php

$obj = new CategoryList();
if (method_exists($obj, 'init'))
{
	$obj->init();

}

for ($i = 0; $i < count($obj->mCategory); $i++)
{
	echo "<a href=''>";
	echo $obj->mCategory[$i]['name'] . "<br/>";
	echo "</a>";

}


$obj2 = new BrandList();
if (method_exists($obj2, 'init'))
{
	$obj2->init();

}
for ($i = 0;$i < count($obj2->mBrand); $i++)
{
	echo "<a href=''>";
	echo $obj2->mBrand[$i]['name'] . "<br/>";
	echo "</a>";

}

?>

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.