hahaitwork Posted October 23, 2012 Share Posted October 23, 2012 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 ? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 23, 2012 Share Posted October 23, 2012 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> Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 23, 2012 Author Share Posted October 23, 2012 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 23, 2012 Share Posted October 23, 2012 Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 23, 2012 Author Share Posted October 23, 2012 am I that bad ? :\ Quote Link to comment Share on other sites More sharing options...
MDCode Posted October 23, 2012 Share Posted October 23, 2012 when you get a redeclare error it is most likely you are calling a file(s) with the same function twice. You can not require functions twice Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 23, 2012 Author Share Posted October 23, 2012 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" ? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 23, 2012 Share Posted October 23, 2012 He was giving you and example of how to do it yourself, not finished code for you to copy. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 23, 2012 Share Posted October 23, 2012 (edited) 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 October 23, 2012 by Barand Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 23, 2012 Author Share Posted October 23, 2012 (edited) 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 October 23, 2012 by hahaitwork Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 24, 2012 Author Share Posted October 24, 2012 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 24, 2012 Share Posted October 24, 2012 Substitute your own products() and others() functions for my dummy ones. Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 25, 2012 Author Share Posted October 25, 2012 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) Quote Link to comment Share on other sites More sharing options...
Barand Posted October 25, 2012 Share Posted October 25, 2012 or just remove my functions and include your file Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 25, 2012 Author Share Posted October 25, 2012 or just remove my functions and include your file That worked well But Is it possible to make a menu ? where buttons don't switch, it just switch the contents ? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 25, 2012 Share Posted October 25, 2012 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. Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 25, 2012 Author Share Posted October 25, 2012 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 Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 25, 2012 Author Share Posted October 25, 2012 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.. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 25, 2012 Share Posted October 25, 2012 <?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> Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 26, 2012 Author Share Posted October 26, 2012 <?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 ? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 26, 2012 Share Posted October 26, 2012 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 Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 26, 2012 Author Share Posted October 26, 2012 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] Quote Link to comment Share on other sites More sharing options...
Barand Posted October 26, 2012 Share Posted October 26, 2012 http://forums.phpfreaks.com/topic/1895-header-errors-read-here-before-posting-them/ Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 26, 2012 Author Share Posted October 26, 2012 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. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 26, 2012 Share Posted October 26, 2012 Read the link Barand posted above. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.