zirgs Posted May 16, 2006 Share Posted May 16, 2006 Hello there, i was coding pretty well till now ... because i can't understand one thing ...so i have some mysql table newsand there is some fields like id number and messagei was vondering on how to make like the switch script work like that i dont need to write each id in the script but it somehow calculates himselfti mean like if user enters news?id=36 then script shows new from mysql with that id but if the id is incorrect just show the main page for news ... so any help?thanks .... Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/ Share on other sites More sharing options...
zirgs Posted May 17, 2006 Author Share Posted May 17, 2006 any help please ... ? it would be great ... Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-36638 Share on other sites More sharing options...
alpine Posted May 18, 2006 Share Posted May 18, 2006 I'm not sure if this is what u are looking for, but here is one example to work with. Ofcourse you have to change the table name and col names and work with what and how you want things to appear to the userIf this was NOT what u where looking for, please post you current and relevant code[code]<?$id = $_GET['id'];settype($id,"integer");if(!empty($_GET['id'])){$sql = mysql_query("select * from table_news where id = '$id'") or die(mysql_error());$num = mysql_num_rows($sql);if($num == 1){$row = mysql_fetch_array($sql);$title = $row["title"];echo $title; // etc}}if(empty($_GET['id']) OR $num <> 1){$sql = mysql_query("select * from table_news order by id desc") or die(mysql_error());while($row = mysql_fetch_array($sql)){ echo $row["title"]; echo "<br />";}}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-36824 Share on other sites More sharing options...
zirgs Posted May 21, 2006 Author Share Posted May 21, 2006 It seems that you didn't got what i wanted ... ok so ...now i'm using this code[code]<?php $id = $_GET['id'];switch($id) { case '1': include_once('1.txt'); break; case '2': include_once('2.txt'); break; default: include_once('news.txt');}?>[/code]and i want the same but with mysql not .txt files ... but i don't want to write each case'' into the script but make it calculate himself ... if case'' is 1 then select id 1 if case'' is 3098 then select 3098 ... that's what i want ... and if id is wrong then display the default: ... so can you please help with with that ? Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-37619 Share on other sites More sharing options...
alpine Posted May 21, 2006 Share Posted May 21, 2006 Well, if you have news in a mysql table, my posted code pretty much does what you need to get news - fetching news from mysql isn't going through a switch statement - it is normally fetched based on the $id.What is your table structure then ? Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-37635 Share on other sites More sharing options...
zirgs Posted May 21, 2006 Author Share Posted May 21, 2006 IDnewstitlesdates Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-37639 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 writing a switch statement for what you want doesn't make sense, because you can check for the id and assign the default if it doesn't exist in the steps you already have to take to get the info. Alpine's example is fine except for i don't think he quite understood you as far as what to do in case of default. Or else, I misunderstood you. I understand it to be that the default is just the current message and id, as opposed to a specific one. So, assuming that i'm currect:[code]$def = 1234; // whatever the id for the current news isif ($_GET['id']) ? $id = $_GET['id'] : $id = $def; $sql = "SELECT * FROM table WHERE id IN ('$id','$def')";$rs = mysql_query($sql);while ($found = mysql_fetch_array($rs)) { if ($found['id'] == $id) { $news_message = $found['message']; break; } else { $news_message = $found['message']; }} [/code]you will have to change the table/column names to your own. basically what happens here is this:$def = 1234; is the default id number for the current news. i leave it up to you to make $def 's value hold that id number. then we check to see if the user entered in an id number. if they did, then we set $id to that number. if not, we set $id to the default id. Then we query the database for all entries with both id numbers (obviously only returning 1 result if the user did not enter one in - assuming id's are unique). Then we check the results of the query. for each loop, we will check to see if the result id equals $id. if it does, then we set $news_message to the message and break out of the while loop. if not, we set it anyways, and continue the loop. The idea is that either a) there is a match, and the loop breaks, and $news_message will hold the message the user was looking for, or b) the loop will end because there are no more rows, in which case $news_message will end up being the default. Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-37643 Share on other sites More sharing options...
zirgs Posted May 21, 2006 Author Share Posted May 21, 2006 Oh thanks a lot guys ... now i understand ... problem was in my english [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] .... now i'm good ... thanks, cheers! Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-37665 Share on other sites More sharing options...
zirgs Posted May 22, 2006 Author Share Posted May 22, 2006 Thank you guys another time ... but give me some more help please ...so i wanted how to make with echo function like the title displays like a link to the full new ... like the title has a link like if it's first new then link is news.php?id=1 and so on ... want to make it write by a script ... any help ... it would be very nice i won't disturb you for some time ... :) Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-37984 Share on other sites More sharing options...
.josh Posted May 22, 2006 Share Posted May 22, 2006 [code]echo "<a href = 'blah.php?id=" . $found['id'] . "'>". $found['title'] . "</a>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-38033 Share on other sites More sharing options...
zirgs Posted May 23, 2006 Author Share Posted May 23, 2006 Ouh seems th script doesnt understand the $found argument ... it's just giving out id= and no number ... Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-38143 Share on other sites More sharing options...
.josh Posted May 23, 2006 Share Posted May 23, 2006 well $found['id'] was just an example. you would have to use the array that you used to fetch the array after you did the query. Quote Link to comment https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/#findComment-38409 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.