Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While loop 1

Status
Not open for further replies.

benrob82

Programmer
Jul 28, 2005
116
GB
Hi can anyone help me.

I need to display the 3 most recent events and then the other remaining events need to be displayed in a drop down

this is what I have so far but the dropdown displays the first of the archived events and then any remaining only appear next to the dropdown.

Please this is killing me

Code:
$count = 0;
$getevent = mysql_query("select id,name,start_date,content,Location_c,objective,status,end_date from campaigns,campaigns_cstm where start_date < NOW() and campaigns.id = campaigns_cstm.id_c and status = 'Complete' and EventType_c = 'Taad' order by start_date desc"); 
while ($gotevent = mysql_fetch_row($getevent)) 
{
$count++;

	if ($count <= 3)
	{
		$date = $gotevent[7];
		
		  $date_year=substr($date,0,4); 
		  $date_month=substr($date,5,2); 
		  $date_day=substr($date,8,2); 
		
		$date=date("l jS F Y", mktime(0,0,0,$date_month,$date_day,$date_year)); 
		
		#<a href='viewevents.php?eventid=$gotevent[0]'></a>
		echo "<h2>$gotevent[1]</h2><p><strong>Date: </strong>$date</br><strong>Venue: </strong>$gotevent[4]</p>";
		
		echo "<p>$gotevent[3]</p><p><a href='viewevents.php?eventid=$gotevent[0]'><img src='/templates/taad/images/view.gif' alt='view story' width='43' height='15' border='0'></a><br><br><img src='/templates/taad/images/97.jpg'><br></p>";
	}
	
	else 
	{
		echo"<form name='form1' method='post' action='viewevents.php?eventid=$gotevent[0]'>";
		echo"<select name='events'>";
		echo"<option value='$gotevent[0]'>$gotevent[1]</option>";
		
	}
}
		echo"</select><br><input type='submit' value='Go'></form>";
 
Code:
$count = 0;
$option = "";
$getevent = mysql_query("select id,name,start_date,content,Location_c,objective,status,end_date from campaigns,campaigns_cstm where start_date < NOW() and campaigns.id = campaigns_cstm.id_c and status = 'Complete' and EventType_c = 'Taad' order by start_date desc"); 
while ($gotevent = mysql_fetch_row($getevent)) 
{
$count++;

    if ($count <= 3)
    {
        $date = $gotevent[7];
        
          $date_year=substr($date,0,4); 
          $date_month=substr($date,5,2); 
          $date_day=substr($date,8,2); 
        
        $date=date("l jS F Y", mktime(0,0,0,$date_month,$date_day,$date_year)); 
        
        #<a href='viewevents.php?eventid=$gotevent[0]'></a>
        echo "<h2>$gotevent[1]</h2><p><strong>Date: </strong>$date</br><strong>Venue: </strong>$gotevent[4]</p>";
        
        echo "<p>$gotevent[3]</p><p><a href='viewevents.php?eventid=$gotevent[0]'><img src='/templates/taad/images/view.gif' alt='view story' width='43' height='15' border='0'></a><br><br><img src='/templates/taad/images/97.jpg'><br></p>";
    }
    
    else 
    {
        $option .= "<option value=\"$gotevent[0]\">$gotevent[1]</option>\r\n";
        
    }
}
        echo"<form name='form1' method='post' action='viewevents.php?eventid=$gotevent[0]'>";
        echo"<select name='events'>";
	echo $option;
        echo"</select><br><input type='submit' value='Go'></form>";
 
the problem is you are lopping through the entire result set. So your drop down is doing multiple <forms> instead of adding to just one form and one drop down.

the ELSE needs to only show the form element one time. Maybe if $count=4 then echo "<form" "select" <option first option... ELSE option
at the end of your while add your </select> piece.
 
thanks jpadie this worked a dream apart from not linking to the page but at least the dropdown now contains all of the events.
 
i see. you can't use forms like that. instead test the value of the dropdown field on submission and direct the browser accordingly.
 
the dropdown isn't carrying the value across any idea why, it was before when the dropdown didn't contain the correct results.
 
try this
Code:
echo"<form name='form1' method='post' action='viewevents.php'>";
        echo"<select name='events'>";
    echo $option;
        echo"</select><br><input name=\"submit\" type='submit' value='Go'></form>";
and add to the viewevents.php

Code:
<?
if (isset($_POST['submit'])):
 echo "<pre>";
 print_r($_POST);
 echo "</pre>";
endif;
?>
 
this will display the id on the viewevents.php but how will i use this to pass the variable across
 
pass it to where? it is usable in the same script by referencing $_POST['events'].

to use it on subsequent pages you can pass these on dynamically either through a field in a form (if you don't want to display it then you set the type to hidden), via a query parameter in a url anchor or via sessions.

try one or more of the following:

Code:
session_start();
if ( isset($_POST['events'])):
  $type = "an incoming posted form";
  $eventid = $_POST['events'];
elseif (isset($_GET['events'])):
  $type = "a query string variable or an incoming form with a get method";
  $eventid = $_GET['events'];
elseif (isset($_SESSION['events'])):
  $type = "from an existing session value when the script has not received any other input";
  $eventid = $_GET['events'];
else:
  $eventid = NULL;
endif;

//assign to session value
if (!empty($eventid)):
  $_SESSION['eventid'] = $eventid;
endif;
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
 Enter a value and click submit to assign this to eventid <br/>
 <input type="text" name="events" value="<?=$eventid?>">
 <br/>
 <input type="submit" name="submit" value="submit" />
</form>
<br/>
<br/>
<a href="<?=$_SERVER['PHP_SELF']?>?events=<?=$eventid?>>Click to assign to event <?=$eventid?></a>
<br/>
<br/>
or just click <a href="<?=$_SERVER['PHP_SELF']?>">here</a> to show how things can be passed by sessions

<? if (empty($eventid)): ?>
 No eventid has been set
<? else: ?>
The eventid is <?=$eventid?> and was received from <?=$type?>
<? endif; ?>
 
sorry my fault your first answer solved the problem it was my fault i hadn't setup the viewevents.php file correctly to capture the id.

thanks for all your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top