Jump to content

Recommended Posts

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 ....
Link to comment
https://forums.phpfreaks.com/topic/9778-select-right-id-based-on-esntered-info/
Share on other sites

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
[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]
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 ?
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 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'];
   }
}        
[/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.
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 ... :)
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.