Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    220

    Calendario e link per query

    Ciao a tutti, ho questo script in js che mi crea un calendario che cliccando sulla data effettua una query ad un database Mysql, adesso vorrei che sulle date dove non ci sono record sul database non mi faccia cliccare, ovvero non mi appaia il link ma solo il testo.
    So di dover effettuare una query mysql in testa alla pagina, ma una volta effettuata come modifico la riga 52 (dove mi mostra i giorni) per fare i modo che se il campo nel db contiene dati mi fa cliccare e accedere successivamente ai dati (qui non ho problemi), oppure che se il campo nel db è vuoto non mi faccia cliccare e mi tolga il link?
    Ho chiesto nel forum php ma mi è stato detto che dovevo postare nel forum js...
    Vi posto il codice del file (calendario.php3)
    Vi ringrazio...
    Ecco il codice:
    codice:
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script Language="JavaScript">
    // Class Calendar
    // Description: Generates a dynamic calendar given a month and year.
    // Usage:
    //   To setup you need to define three stylesheet elements: .caldefault, .calday, .caltoday
    //   Then create a Calendar object, e.g.,
    //     var cal = new Calendar(date, month, year, action);
    //   Where:
    //     date - day of the month, e.g., 31 for March 31
    //     month - number representing the month, e.g., 3 for March
    //     year - e.g., 2001
    //     action - the name of the function you want called if the user clicks on a day
    //				This function will be passed with the arguments date, month, year
    //	 When you want to display the calendary, simply call the Show method, e.g.,
    //	   cal.Show();
    
    // Class Methods
    	
    
    function Calendar_GetString()
    {
    	var firstDate = new Date(this.year,this.month,1);
    	var firstDay = firstDate.getDay();
    	var monthArg = this.month + 1;
    	var calStr = "<TABLE BORDER=1 COLS=7>";
    	calStr += "<TR>";
    	calStr += "<TD COLSPAN=7 ALIGN=center><SPAN class=caldefault><font size=2>"+Calendar.monthName[this.month].toUpperCase()+" "+this.year+"</font></SPAN></TD>";
    	calStr += "</TR>";
    	calStr += "<TR>";
    
    	for (var i=0;i<Calendar.dayName.length;i++) 
    		calStr += "<TD ALIGN=center><SPAN class=caldefault><font size=2>"+Calendar.dayName[i]+"</font></SPAN></TD>";
    	calStr += "</TR>";
    
    	var dayCount = 1;
    	calStr += "<TR>";
    	for (var i=0;i<firstDay;i++) 
    		calStr += "<TD> </TD>";
    
    	//var monthArg = this.month + 1; ----variabile vecchia
    	for (var i=0;i<this.monthDays[this.month];i++)
    	{
    		var styleStr = "calday";
    		if (dayCount==this.date)
    			styleStr = "caltoday";
    			
    		calStr += '<TD ALIGN="center">'+
    					'<SPAN class="'+styleStr+'"><font size=2><a href="calendario.php3?chiave=teschio&mese='+monthArg+'&anno='+this.year+'&giorno='+dayCount+'">'+
    					dayCount+'</a></font></SPAN></A></FONT></TD>';
    		dayCount++;
    		if ((i+firstDay+1)%7==0&&(dayCount<this.monthDays[this.month]+1)) 
    			calStr += "</TR><TR>";
    	}
    
    	var totCells = firstDay+this.monthDays[this.month];
    	for (var i=0;i<(totCells>28?(totCells>35?42:35):28)-totCells;i++) 
    		calStr += "<TD> </TD>"
    	calStr += "</TR>";
    	calStr += "</TABLE>";
    	return calStr;
    }
    
    
    
    function Calendar_Show()
    {
    	var calStr = this.GetString();
    	document.write(calStr);
    }
    
    function Calendar(date,month,year,action)
    {
    	// Properties
    	this.date = date;
    	this.month = month;
    	this.year = year;
    	this.action = action;
    	this.monthDays = new  Array(31,28,31,30,31,30,31,31,30,31,30,3
    1);
    
    	// Leap year test
    	if (((this.year % 4 == 0) && (this.year % 100 != 0)) || (this.year % 400 == 0))
          this.monthDays[1] = 29;
    	else 
    		this.monthDays[1] = 28;
    		
    	// Methods
    	this.Show = Calendar_Show;
    	this.GetString = Calendar_GetString;
    }
    // Static Class Properties
    Calendar.dayName = new Array("Dom","Lun","Mar","Mer","Gio","Ven","Sab");
    Calendar.monthName = new Array("Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");
    
    // Class CalendarSet
    // Description: Keeps track of multiple calendars and has methods for populating popup fields.
    
    function CalendarSet_Show()
    {	
    	var border = '';
    	document.write('<table border="0" cellpadding="0" cellspacing="0"><tr>');
    
    	// Show the top border
    	for (var i=0;i<this.calendars.length+2;i++)
    	{
    		document.write(border);
    	}
    	document.write('</tr><tr>');
    
    	document.write(border);
    	for (var i=0;i<this.calendars.length;i++)
    	{
    		document.write('<td>');
    		this.calendars[i].Show();
    		document.write('</td>');
    	}
    	document.write(border);
    	document.write('</tr><tr>');
    	
    	// Show the bottom border
    	for (var i=0;i<this.calendars.length+2;i++)
    	{
    		document.write(border);
    	}
    	document.write('</tr></table>');
    }
    
    
    function  CalendarSet_SetMonthOptions(selectField)
    
    {
    	var selected = true;
    	selectField.options.length = 0; // Clear the popup
    	
    	for (var i=0;i<this.calendars.length;i++)
    	{
    		var anOption = new Option(Calendar.monthName[this.calendars[i].month]+" "+this.calendars[i].year,(this.calendars[i].month+1)+","+this.calendars[i].year,selected,selected);
    		selectField.options[i] = anOption;
    	
    		selected = false;
    	}
    	selectField.options[0].selected = true;
    }
    
    function  CalendarSet_SetDateOptions(monthSelectFi
    eld,dateSelectField)
    {
    	var calObject = this.calendars[monthSelectField.selectedIndex];
    	var daysInMonth = calObject.monthDays[calObject.month];
    	
    	if (dateSelectField.options.length==0)
    	{
    		var anOption = new Option("Date",0,true,true);
    		dateSelectField.options[0] = anOption;	
    	}
    	
    	if (dateSelectField.options.length > daysInMonth+1)
    	{
    		// Remove last entries
    		var excess = dateSelectField.options.length - daysInMonth - 1;
    		for (var i=0;i<excess;i++)
    		{
    			dateSelectField.options[dateSelectField.length-1] = null;
    		}
    	}
    	else if (dateSelectField.options.length < daysInMonth+1)
    	{
    		// Add entries
    		var deficit = daysInMonth - dateSelectField.options.length + 1;
    		for (var i=0;i<deficit;i++)
    		{
    			var anOption = new Option(dateSelectField.options.length,dateSelectField.options.length,false,false);
    			dateSelectField.options[dateSelectField.options.length] = anOption;
    		}
    	}
    	
    	// Set the default day to current
    	var now = new Date();
    	dateSelectField.options[now.getDate()].selected = true;
    }
    
    function CalendarSet(startDate,months,action)
    {
    	// Properties
    	this.borderColor = "#42316B";
    	this.calendars = new Array();
    
    	var now = new Date();
    	var month=startDate.getMonth();
    	var year = startDate.getFullYear();
    	
    	for (var i=0;i<months;i++)
    	{
    		var dayToHilite = 0;
    		if ((now.getFullYear()==year)&&(now.getMonth()==month))
    			dayToHilite = now.getDate();
    		this.calendars[i] = new  Calendar(dayToHilite,month,year,action);
    
    		month++;
    		if (month>=12)
    		{
    			month = 0;
    			year++;
    		}
    	}
    
    	// Methods
    	this.Show = CalendarSet_Show;
    	this.SetMonthOptions = CalendarSet_SetMonthOptions;
    	this.SetDateOptions = CalendarSet_SetDateOptions;
    }
    
    </script>
      
    </head>
    
    <body bgcolor="#FFFFFF" text="#000000" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">
    <?php 
    $host= "localhost";
    $user= "USER";
    $password= "PASSWORD";
    $dbname= "DBNAME";
    $link = mysql_connect ($host, $user, $password);
    $tutto =  "SELECT * FROM TABELLA"; 
    $query = mysql_db_query ($dbname,$tutto) 
    or die ( "Non riesco ad eseguire la richiesta $tutto"); 
    $righe = mysql_num_rows ($query); 
    ?> 
    <script Language="JavaScript">
    // Example Calendar Click Handler
    function HandleCalClick(day,mon,year)
    {
    	//alert("Got a click on " + mon + "/" + day + "/" + year);
            document.write('" + mon + "/" + day + "/" + year');
    }
    
    // Setup
    var gImageDir = "images/"
    
    // Create calendar objects
    var now=new Date();
    var gCalendarSet = new CalendarSet(now,1,"HandleCalClick");
    
    gCalendarSet.Show();
    
    </script>
     <? 
    if (isset($chiave)){ 
    $dati=mysql_db_query( "$dbname", "SELECT * FROM TABELLA WHERE CAMPO BETWEEN '%$anno%-%$mese%-%$giorno% 00:00:00' AND '%$anno%-%$mese%-%$giorno% 23:59:59' ORDER BY CAMPO DESC"); 
    while ($row = mysql_fetch_array ($dati)){ 
    $verifica=$row[ "title"];  
        echo "
    Titolo: ".$row[ "title"]. " Oggetto: ".$row[ "oggetto"]. " "; 
        echo  "[ <a href=\"index.php?module=demo&func=display&lid=$row[lid]&pid=$row[cid]\">".VISUALIZZA."</a> ]
    ";
    } 
    if (!$verifica){ 
        print ( "
    Nessuna corrispondenza trovata"); 
        } 
    mysql_free_result ($dati); 
    }else{ 
        echo  "
    Seleziona una data"; 
    } 
    mysql_free_result ($query); 
    mysql_close ($link); 
    ?> 
    </body>
    </html>

  2. #2
    non mi sono messo a spulciare nel codice , lo ammetto
    immagino che tu cliccando sulla data faccia partire la query che ritorna i risultati.
    in questo modo non puoi controllare PRIMA di cliccare l'esistenza o no di dati relativi a quella data.
    dovresti far si che la pagina verso cui e' diretto il link sull'onclkick della data controlli sul db e che se i valori tornano null allora ti dia un messaggio tipo "non ci sono dati relativi a questa data"

    oppure, al contrario, fai partire la query appena caricata la pagina, cosi' che tramite php tu possa fare un controllo del tipo:

    assegni un attributo (con un ciclo) ad ogni data (immagino sia in un td) contenente il codice identificativo del record che poi dovra' tornare cliccando su quella determinata data(o un qualunque altro elemento che segnali la presenza di un record per quell'elemento), e all'onclick poi controlli se l'attributo e' valorizzato o meno, facendo visualizzare il dettaglio della data o mandando un messaggio di errore in caso non ci sia un record per quell'elemento.

    e' spiegato piu' in prosa che in js pero' se conosci il codice passare da italiano a informatico non dovrebbe essere difficile
    ciaociao
    ----------------------
    i rulez.-.dovrebbero mettere "pippo" come parola riservata in tutti i linguaggi

    The Cyberpunk System One
    http://www.geocities.com/notoleranceforyou/Hymagition.html

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    220
    Ti ringrazio innanzitutto per avermi risposto!
    Il problema è che la teoria la conosco (cioè effettuare una query al database all'apertura della pagina) ma non so come metterla in pratica con js!

  4. #4
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    220
    br1, Xinod un aiutino da voi?

  5. #5
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    220
    Oppss mi sono accorto che c'era una errore nel codice che vi ho dato prima.... (infatti l'ho copiato ed incollatto e non visualizzavo nulla!)
    Questo è il codice corretto, almeno un piccolo aiutino?
    codice:
    <script Language="JavaScript">
    
    function Calendar_GetString()
    {
    	var firstDate = new Date(this.year,this.month,1);
    	var firstDay = firstDate.getDay();
    	var monthArg = this.month + 1;
    	var calStr = "<TABLE BORDER=1 COLS=7>";
    	calStr += "<TR>";
    	calStr += "<TD COLSPAN=7 ALIGN=center><SPAN class=caldefault><font size=2>"+Calendar.monthName[this.month].toUpperCase()+" "+this.year+"</font></SPAN></TD>";
    	calStr += "</TR>";
    	calStr += "<TR>";
    
    	for (var i=0;i<Calendar.dayName.length;i++) 
    		calStr += "<TD ALIGN=center><SPAN class=caldefault><font size=2>"+Calendar.dayName[i]+"</font></SPAN></TD>";
    	calStr += "</TR>";
    
    	var dayCount = 1;
    	calStr += "<TR>";
    	for (var i=0;i<firstDay;i++) 
    		calStr += "<TD> </TD>";
    
    
    	for (var i=0;i<this.monthDays[this.month];i++)
    	{
    		var styleStr = "calday";
    		if (dayCount==this.date)
    			styleStr = "caltoday";
    			
    		calStr += '<TD ALIGN="center">'+
    					'<SPAN class="'+styleStr+'"><font size=2><a href="calendario.php3?chiave=teschio&mese='+monthArg+'&anno='+this.year+'&giorno='+dayCount+'">'+
    					dayCount+'</a></font></SPAN></A></FONT></TD>';
    		dayCount++;
    		if ((i+firstDay+1)%7==0&&(dayCount<this.monthDays[this.month]+1)) 
    			calStr += "</TR><TR>";
    	}
    
    	var totCells = firstDay+this.monthDays[this.month];
    	for (var i=0;i<(totCells>28?(totCells>35?42:35):28)-totCells;i++) 
    		calStr += "<TD> </TD>"
    	calStr += "</TR>";
    	calStr += "</TABLE>";
    	return calStr;
    }
    
    
    
    function Calendar_Show()
    {
    	var calStr = this.GetString();
    	document.write(calStr);
    }
    
    function Calendar(date,month,year,action)
    {
    
    	this.date = date;
    	this.month = month;
    	this.year = year;
    	this.action = action;
    	this.monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    
    
    	if (((this.year % 4 == 0) && (this.year % 100 != 0)) || (this.year % 400 == 0))
          this.monthDays[1] = 29;
    	else 
    		this.monthDays[1] = 28;
    		
    
    	this.Show = Calendar_Show;
    	this.GetString = Calendar_GetString;
    }
    
    Calendar.dayName = new Array("Dom","Lun","Mar","Mer","Gio","Ven","Sab");
    Calendar.monthName = new Array("Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");
    
    
    
    
    function CalendarSet_Show()
    {	
    	var border = '';
    	document.write('<table border="0" cellpadding="0" cellspacing="0"><tr>');
    
    
    	for (var i=0;i<this.calendars.length+2;i++)
    	{
    		document.write(border);
    	}
    	document.write('</tr><tr>');
    
    	document.write(border);
    	for (var i=0;i<this.calendars.length;i++)
    	{
    		document.write('<td>');
    		this.calendars[i].Show();
    		document.write('</td>');
    	}
    	document.write(border);
    	document.write('</tr><tr>');
    	
    
    	for (var i=0;i<this.calendars.length+2;i++)
    	{
    		document.write(border);
    	}
    	document.write('</tr></table>');
    }
    
    
    function CalendarSet_SetMonthOptions(selectField)
    {
    	var selected = true;
    	selectField.options.length = 0; 
    	
    	for (var i=0;i<this.calendars.length;i++)
    	{
    		var anOption = new Option(Calendar.monthName[this.calendars[i].month]+" "+this.calendars[i].year,(this.calendars[i].month+1)+","+this.calendars[i].year,selected,selected);
    		selectField.options[i] = anOption;
    	
    		selected = false;
    	}
    	selectField.options[0].selected = true;
    }
    
    function CalendarSet_SetDateOptions(monthSelectField,dateSelectField)
    {
    	var calObject = this.calendars[monthSelectField.selectedIndex];
    	var daysInMonth = calObject.monthDays[calObject.month];
    	
    	if (dateSelectField.options.length==0)
    	{
    		var anOption = new Option("Date",0,true,true);
    		dateSelectField.options[0] = anOption;	
    	}
    	
    	if (dateSelectField.options.length > daysInMonth+1)
    	{
    
    		var excess = dateSelectField.options.length - daysInMonth - 1;
    		for (var i=0;i<excess;i++)
    		{
    			dateSelectField.options[dateSelectField.length-1] = null;
    		}
    	}
    	else if (dateSelectField.options.length < daysInMonth+1)
    	{
    
    		var deficit = daysInMonth - dateSelectField.options.length + 1;
    		for (var i=0;i<deficit;i++)
    		{
    			var anOption = new Option(dateSelectField.options.length,dateSelectField.options.length,false,false);
    			dateSelectField.options[dateSelectField.options.length] = anOption;
    		}
    	}
    	
    
    	var now = new Date();
    	dateSelectField.options[now.getDate()].selected = true;
    }
    
    function CalendarSet(startDate,months,action)
    {
    
    	this.borderColor = "#42316B";
    	this.calendars = new Array();
    
    	var now = new Date();
    	var month=startDate.getMonth();
    	var year = startDate.getFullYear();
    	
    	for (var i=0;i<months;i++)
    	{
    		var dayToHilite = 0;
    		if ((now.getFullYear()==year)&&(now.getMonth()==month))
    			dayToHilite = now.getDate();
    		this.calendars[i] = new Calendar(dayToHilite,month,year,action);
    		month++;
    		if (month>=12)
    		{
    			month = 0;
    			year++;
    		}
    	}
    
    
    	this.Show = CalendarSet_Show;
    	this.SetMonthOptions = CalendarSet_SetMonthOptions;
    	this.SetDateOptions = CalendarSet_SetDateOptions;
    }
    
    </script>
      
    </head>
    
    <body bgcolor="#FFFFFF" text="#000000" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">
    <?php 
    $host= "localhost";
    $user= "USER";
    $password= "PASSWORD";
    $dbname= "DBNAME";
    $link = mysql_connect ($host, $user, $password);
    $tutto =  "SELECT * FROM TABELLA"; 
    $query = mysql_db_query ($dbname,$tutto) 
    or die ( "Non riesco ad eseguire la richiesta $tutto"); 
    $righe = mysql_num_rows ($query); 
    ?> 
    <script Language="JavaScript">
    
    function HandleCalClick(day,mon,year)
    {
    
            document.write('" + mon + "/" + day + "/" + year');
    }
    
    
    var gImageDir = "images/"
    
    
    var now=new Date();
    var gCalendarSet = new CalendarSet(now,1,"HandleCalClick");
    gCalendarSet.Show();
    
    </script>
    <? 
    if (isset($chiave)){ 
    $dati=mysql_db_query( "$dbname", "SELECT * FROM TABELLA WHERE CAMPO BETWEEN '%$anno%-%$mese%-%$giorno% 00:00:00' AND '%$anno%-%$mese%-%$giorno% 23:59:59' ORDER BY CAMPO DESC"); 
    while ($row = mysql_fetch_array ($dati)){ 
    $verifica=$row[ "title"];  
        echo "
    Titolo: ".$row[ "title"]. " Oggetto: ".$row[ "oggetto"]. " "; 
        echo  "[ <a href=\"index.php?module=demo&func=display&lid=$row[lid]&pid=$row[cid]\">".VISUALIZZA."</a> ]
    ";
    } 
    if (!$verifica){ 
        print ( "
    Nessuna corrispondenza trovata"); 
        } 
    mysql_free_result ($dati); 
    }else{ 
        echo  "
    Seleziona una data"; 
    } 
    mysql_free_result ($query); 
    mysql_close ($link); 
    ?> 
    </body>

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.