How to make my rows within different html tables selectable using PHP? - php

I am new to PHP and currently I am using php to create four different conference tables that have some hard-coded data in for now and I am trying to make any of the rows within the four tables selectable and eventually I will save the rows selected into a database, but for now I just can't figure out a way to make the rows selectable using PHP. Below is the current code that displays the four tables on my localhost. If this is not possible with PHP how would I incorporate another language within a php file to make the rows selectable. Thank you all for your help in advance.
Conference Class:
<?php
class Conference
{
protected $teams;
function loadTeams($teams)
{
$this->teams = $teams;
}
function getTeams()
{
return $this->teams;
}
}
?>
Main code:
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>') ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>User selection page</title>
<link rel="stylesheet" href="gameViewStyleSheet.css" type="text/css" />
</head>
<?php
require_once('Conference.php');
for ($j = 0; $j<4; $j++)
{
$loadGameClass = new Conference();
$loadGameClass->loadTeams(array("(1)Gonzaga vs (16)Southern U", "(8)Pittsburgh vs (9)Wichita St", "(5)Wisconsin vs (12)Ole Miss", "(4)Kansas st vs (13)Boise St", "(6)Arizona vs (11)Belmont", "(3)New Mexico vs (14) Harvard", "(7)Notre Dame vs (10)Iowa St", "(2)Ohio St vs (15) Iona"));
$teams = $loadGameClass->getTeams();
echo '<table border="1" align="center">';
switch ($j) {
case 0:
echo "Midwest";
break;
case 1:
echo "West";
break;
case 2:
echo "South";
break;
case 3:
echo "East";
break;
}
for ($i = 0; $i < 8; $i++)
{
$games = $teams[$i];
echo '<tr><td>'.$games.'</td><tr>';
}
echo '</table>';
echo "<br>" . "<br>";
}
?>
<body>
</body>
</html>

If you're talking "selectable" as in front-end interactivity in your browser, that cannot be done with PHP. You need to use JavaScript.
If you just want a link that would tag something as "selected" with a page load and a session value, you could do small form submits for each select/deselect action, and highlight the appropriate rows.
side note: <br> is invalid in XHTML - you need to use <br />

Related

PHP 6/MySQL Programming for the Absolute Beginner petals.php

I'm a newbie here and probably my question has already been answered, but Andy Harris wrote a book (see post topic for name of book). I've enjoyed going through it, however I've got a question that I'd like to post here (I've reached out to him, but have not got a response). The code in question is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>petals.php</title>
<link rel = "stylesheet"
type = "text/css"
href = "petals.css" />
</head>
<body>
<h1>Petals Around the Rose</h1>
<?php
printGreeting();
printDice();
printForm();
//$numPetals = filter_input(INPUT_POST, "numPetals");
function printGreeting(){
global $numPetals;
$guess = filter_input(INPUT_POST, "guess");
$numPetals = filter_input(INPUT_POST, "numPetals");
if (!filter_has_var(INPUT_POST, "guess")){
print "<h3>Welcome to Petals Around the Rose</h3>";
} else if ($guess == $numPetals){
print "<h3>You Got It!</h3>";
} else {
print <<<HERE
<h3>from last try: </h3>
<p>
you guessed: $guess
</p>
<p>
-and the correct answer was: $numPetals petals around the rose
</p>
HERE;
} // end if
} // end printGreeting
function showDie($value){
print <<<HERE
<img src = "die$value.jpg"
height = "100"
width = "100"
alt = "die: $value" />
HERE;
} // end showDie
function printDice(){
global $numPetals;
print "<h3>New Roll:</h3> \n";
$numPetals = 0;
$die1 = rand(1,6);
$die2 = rand(1,6);
$die3 = rand(1,6);
$die4 = rand(1,6);
$die5 = rand(1,6);
print "<p> \n";
showDie($die1);
showDie($die2);
showDie($die3);
showDie($die4);
showDie($die5);
print "</p> \n";
calcNumPetals($die1);
calcNumPetals($die2);
calcNumPetals($die3);
calcNumPetals($die4);
calcNumPetals($die5);
} // end printDice
function calcNumPetals($value){
global $numPetals;
switch ($value) {
case 3:
$numPetals += 2;
break;
case 5:
$numPetals += 4;
break;
} // end switch
} // end calcNumPetals
function printForm(){
global $numPetals;
print <<<HERE
<h3>How many petals around the rose?</h3>
<form action = ""
method = "post">
<fieldset>
<input type = "text"
name = "guess"
value = "0" />
<input type = "hidden"
name = "numPetals"
value = "$numPetals" />
<br />
<input type = "submit" />
</fieldset>
</form>
<p>
<a href = "petalHelp.html">
give me a hint</a>
</p>
HERE;
} // end printForm
?>
</body>
</html>
He says in his book (p. 95 for those who have the text):
This function [printGreeting()] refers to both the $guess and
$numPetals variables. Since both may be needed by other functions,
they are defined in a global statement. Note that you can assign
global status to more than one variable in a single global command.
I don't see where they are defined in a global statement. Probably overthinking it, but any insight would be much appreciated.
the 'statement' global defines the variable as having a global scope even though it is in the function so anything referencing $numPetals will access the same value.
it's possible that you will add code later in the tutorial that uses the $numPetals variable outside of a function but this may be the first introduction of the concept of 'global' in the book which is why it was explained.
in PHP a variable doesn't necessarily have to be defined before it's used which may be the cause of the confusion.

Error on generating chart

In this below code i want to generate pie chart.and i got this error Undefined variable: mysqli, Call to a member function query() on a non-object at line 29.Please any one help me to rectify the problem to get solution.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Pie Chart Demo (LibChart)- http://codeofaninja.com/</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" />
</head>
<body>
<?php
//include the library
include "libchart/libchart/classes/libchart.php";
//new pie chart instance
$chart = new PieChart( 500, 300 );
//data set instance
$dataSet = new XYDataSet();
//actual data
//get data from the database
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from programming_languages";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
if( $num_results > 0){
while( $row = $result->fetch_assoc() ){
extract($row);
$dataSet->addPoint(new Point("{$name} {$ratings})", $ratings));
}
//finalize dataset
$chart->setDataSet($dataSet);
//set chart title
$chart->setTitle("Tiobe Top Programming Languages for June 2012");
//render as an image and store under "generated" folder
$chart->render("generated/1.png");
//pull the generated chart where it was stored
echo "<img alt='Pie chart' src='generated/1.png' style='border: 1px solid gray;'/>";
}else{
echo "No programming languages found in the database.";
}
?>
</body>
</html>
you should not end the <form> until all fields has been printed.
you have a
</form>
<select>....</select>
please output the select first an then close the form. Otherwise the value of your select field will not be submited.

Make PHP Click Link On Same PAge Or Process A Form

Is there any php code i can use to click a link or process a form on the page the php is on?
Im building a redirect script and what i need to do is use php to move user to next page, its mandatory that php has to be used html doesnt work. In it i have a self submit forum but it doesnt work how i load the script. Is there a way i can use php code to submit it? or remove it and put a link there then use php to click that link?
This is the code below:
if ($_GET['ref_spoof'] != NULL)
{
$offer = urldecode($_GET['ref_spoof']);
$p1 = strpos ($offer, '?') + 1;
$url_par = substr ($offer , $p1);
$paryval = split ('&', $url_par);
$p = array();
foreach ($paryval as $value)
{
$p[] = split ('=',$value);
}
//header('Location: '.$offer.'') ;
print
'
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">$("#mylink").click()</script>
Index Page
<script type="text/javascript">$("#mylink").click()</script>
<script type="text/javascript">document.getElementById("myLink").click();</script>
<form action="'.$offer.'" method="get" id="myform">
';
foreach ($p as $value)
{
echo '<input type="hidden" name="'.$value[0].'" value="'.$value[1].'">';
}
echo '</form><script language="JavaScript"> document.getElementById(\'myform\').submit();</script></body></html>';
}
Looks like you're trying to make this too complicated.
You're loading a page that submits a form using GET.
Is there any reason you can't use
header("Location : ".$offer."?".http_build_query($p));
http_build_query being a function to generate an URL string from an array. Assuming $p is the array containing all form fieldnames+values.
example of http_build_query:
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data);
will result in:
foo=bar&baz=boom&cow=milk&php=hypertext+processor

Passing html form values to php file

Whether I enter the value for bug id or not ..in both conditions the code between php tags is displayed as output. Can someone help me to find out the reason.
Code is given below:
html file-------------------------------------------------------------
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Bug Report</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>Bug Report</h2>
<form action="test.php" method="post" >
<p>Bug ID:<input type="text" name="bugid" size="20" /></p>
<p><input type="submit" value="Record Bug" /></p>
</form>
</body>
</html>
php file--------------------------------------------------
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Record Bug</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$bugid=$_POST["bugid"];
echo $bugid;
if (empty($bugid))
{
echo "<p>You must enter the Bug ID to record the bug.</p>";
}
else
{
echo"<p>good</p>";
}
?>
</body>
</html>
If you're getting PHP code in the output, then your webserver isn't running that page/script through the PHP interpreter. Generally, that's because you've put the code into a .html file, which is not treated as PHP by default.
Either rename the file to whatever.php, or reconfigure your webserver to treat .html files as PHP scripts.
check that php is working or not for that write the code <?php phpinfo(); ?> and if have manually installed php apache and getting problem try wamp server
your code is widely open for sql-injunction to make it secure use
public function mysql_prep( $value ) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
if( $new_enough_php ) { // PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if( $magic_quotes_active ) { $value = stripslashes( $value ); }
$value = mysql_real_escape_string( $value );
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if( !$magic_quotes_active ) { $value = addslashes( $value ); }
// if magic quotes are active, then the slashes already exist
}
return $value;
}
Check whether php is running or not in your machine. Save the below code as test.php and run it through
<?php
phpinfo();
?>
In that case you have to run on that Server which support PHP like Xampp or Wamp and also extension of the file should be .php

Colour Extract Script - ForEach Error

So I downloaded and edited a script off the internet to pull an image and find out the hex values it contains and their percentages:
The script is here:
<?php
$delta = 5;
$reduce_brightness = true;
$reduce_gradients = true;
$num_results = 5;
include_once("colors.inc.php");
$ex=new GetMostCommonColors();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Colour Verification</title>
</head>
<body>
<div id="wrap">
<img src="http://www.image.come/image.png" alt="test image" />
<?php
$colors=$ex->Get_Color("http://www.image.come/image.png", $num_results, $reduce_brightness, $reduce_gradients, $delta);
$success = true;
foreach ( $colors as $hex => $count ) {
if ($hex !== 'e6af23') {$success = false; }
if ($hex == 'e6af23' && $count > 0.05) {$success = true; break;}
}
if ($success == true) { echo "This is the correct colour. Success!"; } else { echo "This is NOT the correct colour. Failure!"; }
?>
</div>
</body>
</html>
Here is a pastebin link to the file colors.inc.php
http://pastebin.com/phUe5Pad
Now the script works absolutely fine if I use an image that is on the server, eg use /image.png in the Get_Color function. However, if I try and use an image from another website including a http://www.site.com/image.png then the script no longer works and this error appears:
Warning: Invalid argument supplied for foreach() in ... on line 22
Is anyone able to see a way that I would be able to hotlink to images because this was the whole point of using the script!
You must download a file to the server and pass its full filename to the method Get_Color($img) as $img parameter.
So, you need to investigate another SO question: Download File to server from URL
The error indicates that the value returned by Get_Color is not a valid object that can be iterated on, probably not a collection. You need to know how the Get_Color works internally and what is returned when it doesn't get what it wants.
In the mean-time, you can download [with PHP] the image from the external url into your site, and into the required folder and read the image from there.
$image = "http://www.image.come/image.png";
download($image, 'folderName'); //your custom function
dnld_image_name = getNameOfImage();
$colors=$ex->Get_Color("/foldername/dnld_image_name.png");
By the way, did you confirm that the image url was correct?

Categories