Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
	else if ($_POST['action'] == 'peukuta_nega')
	{
		// Update -rating to the database
		// prepare query
		$query = $mysql_conn->prepare("UPDATE peukku_viestit SET rating = rating - 1 WHERE id = '" . $_POST['id'] . "'");
		// run query
		$query->execute();
		
		//remove post data by redirecting to ourselves
		header( 'Location: index.php' ) ;
	
	}
	
}

2.2.3 Sisällön muodostaminen

Aloitamme suorittamalla kyselyn tietokannasta. ORDER BY-lause järjestää tiedon haluamallamme tavalla.

Code Block

// prepare the query
$kysely = $mysql_conn->prepare("SELECT * FROM peukku_viestit ORDER BY rating DESC, submit_time DESC");
// run it
$kysely->execute();

While silmukka käy yksitellen jokaisen kyselyn sisältämän rivin läpi. Käytimme PDO::FETCH_ASSOC-ominaisuutta, jotta saisimme lajiteltua tiedon tekstiavaimen eikä numeron sijaan. $rivi"rating" vs $rivi0.

Code Block

// process results from query one row at a time
$i=1;
while ($rivi = $kysely->fetch(PDO::FETCH_ASSOC)) {
	echo "<hr/>" . $i . 
	". <br />Rating: " . $rivi["rating"]
    . " <br />Time: " . $rivi["submit_time"]
    . " <p>Message:<br />" . $rivi["message"]
    ."</p>";
    
    $i++;
}