Jump to content

Load/unload Function


hahaitwork

Recommended Posts

Hello,

 

Was thinking of doing a easy way to load different things

 

I make a few buttons, which are going to do this:

 

Button 1 -> Load Product list 1.

 

Load product list 1 (run this script) -> <?php products(); ?>

 

When you press button 2, I want it to unload <?php products(); ?>

and load <?php others(); ?>

 

When I press button 1 again, I want it to unload <?php others(); ?>

and load <?php products(); ?>

 

If you understand what I mean?

Is this possible to do with buttons and in a easy way without changing the whole file, just a few lines of script to make it work ?

Link to comment
Share on other sites

do you mean something like this example?

 

<?php
function products()
{
   echo "<div style='margin:50px; border: 1px solid gray; padding:50px; font-size:24pt; color:blue'>\n
    Product List
    </div>\n";
}

function other()
{
   echo "<div style='margin:50px; border: 1px solid gray; padding:50px; font-size:24pt; color:green'>\n
    Other List
    </div>\n";
}



$list =  (isset($_GET['list'])) ? $_GET['list'] : 'products';

switch ($list) {
   case 'other':
    echo "<button id='products'>Show Product List</button><br />\n";
    other();
    break;
   case 'products':
    echo "<button id='other'>Show Other List</button><br />\n";
    products();
    break;
}

?>
<script type="text/javascript">
   if (document.getElementById('products'))
    document.getElementById('products').onclick = function() {location.href='?list=products';}
   if (document.getElementById('other'))
    document.getElementById('other').onclick = function() {location.href='?list=other';}
</script>

Link to comment
Share on other sites

do you mean something like this example?

 

<?php
function products()
{
echo "<div style='margin:50px; border: 1px solid gray; padding:50px; font-size:24pt; color:blue'>\n
 Product List
 </div>\n";
}

function other()
{
echo "<div style='margin:50px; border: 1px solid gray; padding:50px; font-size:24pt; color:green'>\n
 Other List
 </div>\n";
}



$list = (isset($_GET['list'])) ? $_GET['list'] : 'products';

switch ($list) {
case 'other':
 echo "<button id='products'>Show Product List</button><br />\n";
 other();
 break;
case 'products':
 echo "<button id='other'>Show Other List</button><br />\n";
 products();
 break;
}

?>
<script type="text/javascript">
if (document.getElementById('products'))
 document.getElementById('products').onclick = function() {location.href='?list=products';}
if (document.getElementById('other'))
 document.getElementById('other').onclick = function() {location.href='?list=other';}
</script>

 

Kind of looks right, but I get this error

Fatal error: Cannot redeclare products() (previously declared in C:\xampp\htdocs\xampp\cartindex.php:31) in C:\xampp\htdocs\xampp\cart.php on line 184

What is on line 184?

}

 

2 things:

The buttons will be placed by me somewhere and they should never be removed, just start a function when you press them.

The database is called "others", not "other" but I can rewrite that after my self thought , just saying if we both give a script and you think I have made a mistake :)

Link to comment
Share on other sites

do you mean something like this example?

 

<?php
function products()
{
echo "<div style='margin:50px; border: 1px solid gray; padding:50px; font-size:24pt; color:blue'>\n
 Product List
 </div>\n";
}

function other()
{
echo "<div style='margin:50px; border: 1px solid gray; padding:50px; font-size:24pt; color:green'>\n
 Other List
 </div>\n";
}



$list = (isset($_GET['list'])) ? $_GET['list'] : 'products';

switch ($list) {
case 'other':
 echo "<button id='products'>Show Product List</button><br />\n";
 other();
 break;
case 'products':
 echo "<button id='other'>Show Other List</button><br />\n";
 products();
 break;
}

?>
<script type="text/javascript">
if (document.getElementById('products'))
 document.getElementById('products').onclick = function() {location.href='?list=products';}
if (document.getElementById('other'))
 document.getElementById('other').onclick = function() {location.href='?list=other';}
</script>

 

So.. I guess the last part is wrong because it is looking for a document insted of just the functions in my other file named "cart.php" ?

Link to comment
Share on other sites

I thought the word example would be a clue. And as my functions do nothing but print a couple of words, just to show which is being called, I thought you might have realised they were just dummy examples and not the finished code. You may be amazed to know I don't have your real functions.

 

Remove my functions and let it call yours. That will clear the "redefinition" error.

 

Oh, and sorry about using "other" and not "others". If you're still stuck I can show you how to fix that :)

 

The last part is not looking for a document, it is javascript code defining what the buttons should do when clicked.

Edited by Barand
Link to comment
Share on other sites

I thought the word example would be a clue. And as my functions do nothing but print a couple of words, just to show which is being called, I thought you might have realised they were just dummy examples and not the finished code. You may be amazed to know I don't have your real functions.

 

Remove my functions and let it call yours. That will clear the "redefinition" error.

 

Oh, and sorry about using "other" and not "others". If you're still stuck I can show you how to fix that :)

 

The last part is not looking for a document, it is javascript code defining what the buttons should do when clicked.

 

My bad, thanks for telling me.. Will check it out tomorrow morning!

Been working with some heavy stuff for my brain lately, making my brain working slow.. I should have understood it >.<

Edited by hahaitwork
Link to comment
Share on other sites

I thought the word example would be a clue. And as my functions do nothing but print a couple of words, just to show which is being called, I thought you might have realised they were just dummy examples and not the finished code. You may be amazed to know I don't have your real functions.

 

Remove my functions and let it call yours. That will clear the "redefinition" error.

 

Oh, and sorry about using "other" and not "others". If you're still stuck I can show you how to fix that :)

 

The last part is not looking for a document, it is javascript code defining what the buttons should do when clicked.

 

hmm, I might miss something but your script seems to be correct ? it has the right functions and I changed all the "other" to "others".

if you mean by "changing functions" , you talk about the "echo" part.. I'm not really sure what to write in there. The function products(); is correct etc.

Link to comment
Share on other sites

Substitute your own products() and others() functions for my dummy ones.

 

So, you mean something like this:

<?php
function products() {

$get = mysql_query ('SELECT id , name, description, price , shipping FROM products WHERE id < 100 AND quantity > 0 ORDER BY id DESC');
if (mysql_num_rows($get) == 0) {
echo "There are no products to display!";
}
else {
$count=0;
while ($get_row = mysql_fetch_assoc($get)) {

	 echo
  "<p style='width:240px; text-align:center; float:left;'>"
  .$get_row['name'].'<br />'
	 . $get_row['description'] .
  '<br />€' . number_format ($get_row['price'], 0). '<br />'
  . '<small>Shipping'.': €' . number_format ($get_row['shipping'], 2).' </small>'. '<br />
  <a href="cart.php?add=' .$get_row['id']. '">
  <img src="add.png" border="0" style="height: 25px; width: 50px" ></a></p>';

	 if (++$count % 4 == 0) echo "<div style='clear:both'></div>";
}
echo "<div style='clear:both'></div>";
}
}
function others() {

$get = mysql_query ('SELECT id , name, description, price , shipping FROM products WHERE id >=101 AND id < 200 AND quantity > 0 ORDER BY id DESC');
if (mysql_num_rows($get) == 0) {
echo "There are no products to display!";
}
else {
$count=0;
while ($get_row = mysql_fetch_assoc($get)) {

	 echo
  "<p style='width:240px; text-align:center; float:left;'>"
  .$get_row['name'].'<br />'
	 . $get_row['description'] .
  '<br />€' . number_format ($get_row['price'], 0). '<br />'
  . '<small>Shipping'.': €' . number_format ($get_row['shipping'], 2).' </small>'. '<br />
  <a href="cart.php?add=' .$get_row['id']. '">
  <img src="add.png" border="0" style="height: 25px; width: 50px" ></a></p>';

	 if (++$count % 4 == 0) echo "<div style='clear:both'></div>";
}
echo "<div style='clear:both'></div>";
}
}

$list = (isset($_GET['list'])) ? $_GET['list'] : 'products';
switch ($list) {
case 'other':
	 echo "<button id='products'>Show Product List</button><br />\n";
	 other();
	 break;
case 'products':
	 echo "<button id='other'>Show Other List</button><br />\n";
	 products();
	 break;
}
?>
<script type="text/javascript">
if (document.getElementById('products'))
	 document.getElementById('products').onclick = function() {location.href='?list=products';}
if (document.getElementById('other'))
	 document.getElementById('other').onclick = function() {location.href='?list=other';}
</script>

 

And remove the products and others from my cart.php file ? and yeah.. move it into the index file (the script above is in the index file)

Link to comment
Share on other sites

Move the buttons out of the switch and place them at top of page (or wherever) so they always display. I didn't see the point in having a button to show products when you are already showing the products.

Agree with you but I want maybe 8 buttons or so, and It's just easier and it looks good if they are all there :) if it was only two buttons, it would be nice

Link to comment
Share on other sites

Move the buttons out of the switch and place them at top of page (or wherever) so they always display. I didn't see the point in having a button to show products when you are already showing the products.

 

This shouldn't really be that hard, but I'm a little confused.. this is what i use atm

 <?php
$list = (isset($_GET['list'])) ? $_GET['list'] : 'products';
switch ($list) {
case 'others':
	 echo "<button id='products'>Show Product List</button><br />\n";
	 others();
	 break;
case 'products':
	 echo "<button id='others'>Show others List</button><br />\n";
	 products();
	 break;
}
?>
<script type="text/javascript">
if (document.getElementById('products'))
	 document.getElementById('products').onclick = function() {location.href='?list=products';}
if (document.getElementById('others'))
	 document.getElementById('others').onclick = function() {location.href='?list=others';}
</script>

 

and it works , but I do not manage to move out the buttons without having both buttons and the products/others to be shown.. geh :\

Because as I told you, i need 8 buttons to be shown, and they need to switch out the content , like button 1 is for products, buttons 2 is others etc..

Link to comment
Share on other sites

<?php
echo "<button id='products'>Show Product List</button> ";
echo "<button id='others'>Show others List</button><br />\n";

$list = (isset($_GET['list'])) ? $_GET['list'] : 'products';
switch ($list) {
   case 'others':
				 others();
				 break;
   case 'products':
				 products();
				 break;
}
?>
<script type="text/javascript">
if (document.getElementById('products'))
			 document.getElementById('products').onclick = function() {location.href='?list=products';}
if (document.getElementById('others'))
			 document.getElementById('others').onclick = function() {location.href='?list=others';}
</script>

Link to comment
Share on other sites

<?php
echo "<button id='products'>Show Product List</button> ";
echo "<button id='others'>Show others List</button><br />\n";

$list = (isset($_GET['list'])) ? $_GET['list'] : 'products';
switch ($list) {
case 'others':
				 others();
				 break;
case 'products':
				 products();
				 break;
}
?>
<script type="text/javascript">
if (document.getElementById('products'))
			 document.getElementById('products').onclick = function() {location.href='?list=products';}
if (document.getElementById('others'))
			 document.getElementById('others').onclick = function() {location.href='?list=others';}
</script>

 

Nice, it works!

1 thing: When i press to add anything in "others" into the cart.. it automaticly switch back to products , any idea how i can make it stay on the tab and not switch back to the first when I press add on something ? :)

Link to comment
Share on other sites

A couple of ways

 

1) When you transfer back to that page, pass the $list value. eg

header("location: thispage.php?list=$list");
exit;

 

2) When the list value is passed, store in session var. If no list value is passed, get the value stored in the session var and use that

 

Didn't understand the second one, but I tried the first one and got this error

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\xampp\cart.php:107) in C:\xampp\htdocs\xampp\cartindex.php on line 37

 

I got this code in the cart around the line 37

}
header('Location: '.$page);
}[/b]
[b]if (isset($_GET['remove'])) {
$_SESSION['cart_'.(int)$_GET['remove']]--;
header('Location: '.$page);
}[/b]
[b]if (isset($_GET['delete'])) {
$_SESSION['cart_'.(int)$_GET['delete']]='0';
header('Location: '.$page);
}[/b]
[b]
Link to comment
Share on other sites

A couple of ways

 

1) When you transfer back to that page, pass the $list value. eg

header("location: thispage.php?list=$list");
exit;

 

2) When the list value is passed, store in session var. If no list value is passed, get the value stored in the session var and use that

 

so.. is it right that if I took this in the header, like this:

 

<head>
<?php
header("location: cartindex.php?list=$list");
exit;
?>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

 

Firefox "error" ->

The website does not forward properly

 

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

 

This problem can sometimes because cookies are

denied.

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.