Jump to content


Photo

select right id based on esntered info


  • Please log in to reply
11 replies to this topic

#1 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 16 May 2006 - 02:32 PM

Hello there, i was coding pretty well till now ... because i can't understand one thing ...
so i have some mysql table news
and there is some fields like id number and message
i 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 himselft
i 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 ....

#2 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 17 May 2006 - 03:40 PM

any help please ... ? it would be great ...

#3 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 18 May 2006 - 07:45 AM

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 user
If this was NOT what u where looking for, please post you current and relevant 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 />";
}
}
?>


#4 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 21 May 2006 - 08:41 AM

It seems that you didn't got what i wanted ... ok so ...
now i'm using this 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');
}
?>
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 ?


#5 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 21 May 2006 - 09:30 AM

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 ?

#6 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 21 May 2006 - 09:58 AM

ID
news
titles
dates


#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 10:11 AM

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:
$def = 1234; // whatever the id for the current news is
if ($_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'];
   }
}        

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 21 May 2006 - 11:58 AM

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!

#9 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 22 May 2006 - 04:24 PM

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 ... :)

#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 May 2006 - 08:07 PM

echo "<a href = 'blah.php?id=" . $found['id'] . "'>". $found['title'] . "</a>";

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#11 zirgs

zirgs
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 23 May 2006 - 04:22 AM

Ouh seems th script doesnt understand the $found argument ... it's just giving out id= and no number ...

#12 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 May 2006 - 11:07 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users