-
Posts
16,734 -
Joined
-
Last visited
-
Days Won
9
Everything posted by PFMaBiSmAd
-
Insert data to database where data from while loop
PFMaBiSmAd replied to newphpcoder's topic in PHP Coding Help
1) As previously stated in one of your threads, you need to remove the javascript form submission code. Do NOT try to use javascript to do this until you have enough experience and understand what it is you are doing. Your current page is submitting the form data twice. The javascript is submitting data using the GET method and the browser is submitting the data using the POST method. 2) Also as previously stated in one of your threads, you must use arrays for the form fields so that you can submit the multiple sets of data. Your existing form reuses the same field names and only the last set of data will be submitted. You must use arrays so that each set of data will be submitted. 3) After you get the form to correctly submit the expected sets of data as arrays, then you can write or find php code that loops over that data to process it. I recommend adding the following line of code to your form processing page so that you can see exactly what data is being submitted - echo '<pre>',print_r($_POST,true),'</pre>'; 4) You are also executing a select query inside of a loop using the result of another select query on the same table. That can all be replaced with one query and then loop over the result set from the one query. -
Stuff is working on PHP 5.3.1 but not 5.3.6?
PFMaBiSmAd replied to mastercjb's topic in PHP Coding Help
It's more likely that your message() function is not defined. Have you set error_reporting to E_ALL as suggested along with display_errors to a 1 or ON value? -
Stuff is working on PHP 5.3.1 but not 5.3.6?
PFMaBiSmAd replied to mastercjb's topic in PHP Coding Help
Setting error_reporting to E_ALL would probably help. Setting it to zero will prevent the reporting of all errors. -
Stuff is working on PHP 5.3.1 but not 5.3.6?
PFMaBiSmAd replied to mastercjb's topic in PHP Coding Help
Your forgot to tell us what exactly it is doing, to narrow down the half-dozen different possibilities. Also, what does a 'view source' of the output in your browser show? -
We cannot help you with vague statements about something that does not work. We are not standing right next to you and did not see what you saw when you tried something and contrary to popular belief, the P in HTTP does not stand for Psychic. We don't know what your code is, what your data is, what you did, what result you got, and what result you expected. Without the code that reproduces the problem and a statement or picture of what result you got, and the actual data your code was using, we cannot help you.
-
Edit to the above: You cannot use an odd number of teams. You will end up with teams playing each other more than once and one of the teams won't have an off week.
-
I guess, here's a way you can have 10 weeks. You need 11 teams. The combinations from the code I posted will have a different team playing itself each week. That single team would sit out that week, leaving the remaining 10 teams to play.
-
If you want unique combinations and you want each team to play all the other teams one time, you cannot have 10 weeks. There's an (N-1) term involved in the combinations, so if you have 10 teams (or any combination of teams and bye's that add up to 10), taken two at a time, you have 9 weeks. For 12 teams (or any combination of teams and bye's that add up to 12), taken two at a time, you would have 11 weeks. You should have an even total of teams+bye's so that you don't have a team playing itself (which is essentially sitting out, which is the same as having enough bye's to make an even number.) The only way to have 10 weeks would be to produce more than ten combinations and throw enough away to give you 10 combinations, in which case every team won't play all the other teams one time.
-
It is not efficient to randomly try to schedule teams, because as more and more pairs of the teams have already played, you have less and less valid combinations and by randomly picking combinations, you will end up leaving a pool of teams that have already played that cannot be paired together. You will end up back-tracking and throwing away the sets of picks and when you get near the end, you will find that you must throw away a huge number of tries before you find a set that is unique. The best way of getting a computer to schedule all possible rotations of teams, is to create an array of the teams and shift the data to form all rotations. You would then pick a different set from the result for each schedule. See the following code and the output it produces - <?php $teams = array( "Team 1", "Team 2", "Team 3", "Team 4", "Team 5", "Team 6", "Team 7", "Team 8", "Team 9", "Team 10", "Team 11", "Team 12", "Team 13", "Team 14", "Team 15", "Team 16", "Team 17", "Team 18", "Team 19", "Team 20" ); $numteams = sizeof($teams); $team_list = array_keys($teams); $tmp_list = array_splice($team_list, 1, sizeof($team_list) - 1); $count = sizeof($tmp_list) + 1; $days = array(); for ($i = 0;$i < $numteams - 1;$i++) { $days[$i] = array(); $day_list = array_merge(array($team_list[0]), $tmp_list); for ($j = 0;$j < $count / 2;$j++) { $days[$i][] = $teams[$day_list[$j]] . ' vs ' . $teams[$day_list[$count - $j - 1]]; } // rotate $tmp_list $elm = array_shift($tmp_list); array_push($tmp_list, $elm); } echo "<pre>"; print_r($days); echo "</pre>"; // $days is an array of arrays - 0 - 18 (weeks) and each has 0-9 (games)
-
DISPLAYING an image from MySQL database into HTML table.
PFMaBiSmAd replied to DarnStuckAgain's topic in PHP Coding Help
What you are doing has been done countless times before. Please see replies #4 and #5 at the following link - http://www.phpfreaks.com/forums/index.php?topic=356903.msg1686788#msg1686788 -
end expects an actual array variable as a parameter - You need to assign the result of the explode() statement to a program variable and then supply that as the parameter to the end() statement.
-
You forgot to tell us the actual values your code is using, what result you got, what result you expected, and exactly how you observed that the result is not what you expected. Also, why are you looping over query results where there is only one row in each result set and executing two separate queries against the ffa_matches table just to get the type and skill values from the same row?
-
Restrict access to subfolder / .htaccess
PFMaBiSmAd replied to aSystemOverload's topic in Apache HTTP Server
External css files (<link rel="stylesheet" type="text/css" href="url of a css file" />) and image files (<img src="url of an image">) on a web page are directly requested by the browser and must be accessible through a URL. When you put a .htaccess file into a folder, it affects all subfolders of that folder. If you have a subfolder that you need to allow access to, you would need to put another .htaccess file into that subfolder that has an allow from all statement in it. -
Some error checking and error reporting logic for your code - <?php if(!$query_handle->execute()){ $error = $query_handle->errorInfo(); echo $error[2]; }
-
As ignace stated, you likely don't have error_reporting/display_errors set so that you would be getting any php detected errors. $this->dbc might not be a valid PDO instance. Your prepare() might be failing due to an error in the query statement. What have you done to debug what your code is doing? Using var_dump on $this->dbc and on $query_handle might tell you what is working and what is not.
-
When you build links, for something like pagination, you need to only set the page=x portion of the get parameters. See the following post for how you can do this - http://www.phpfreaks.com/forums/index.php?topic=348834.msg1646676#msg1646676
-
Restrict access to subfolder / .htaccess
PFMaBiSmAd replied to aSystemOverload's topic in Apache HTTP Server
That IS a file system path and the .htaccess file you posted won't stop it from working. Is that everything you put into the .htaccess file that is inside the folder with the files being included? What exact symptom or error did you get the leads you to believe that it prevents the root controller file from including the required subfiles? -
Restrict access to subfolder / .htaccess
PFMaBiSmAd replied to aSystemOverload's topic in Apache HTTP Server
You should be including files using a file system path, not a URL. Using a URL to include files takes at least 10x longer and uses a huge amount of server resources because each file being included causes php to make a http request back to your web server. -
And if you really wanted to maximize the user's experience on your site, you can even highlight the invalid characters when you redisplay the data - <?php if($rep != ''){ // some invalid characters found $search = str_replace('/','\/',addslashes(implode('|',array_unique(str_split($rep))))); // form a replace pattern $string = preg_replace("/($search)/i",'<span class="highlight">[ \1 ]</span>',$_POST['premiumuser_description']); $error_message .= "You must enter a Company Description<br>"; $error_message .= "Your Company Description contained the following invalid characters: <span class='highlight'>$rep</span>, "; $error_message .= "highlighted in the following:<br><br>$string<hr>"; } The css for the above would be - <style type="text/css"> span.highlight {font-weight:bold; color:red;} </style>
-
That validation logic is foobar. The isset() doesn't do anything because text/textarea form fields are set, even if they are empty (you already know the form has been submitted with the isset($_POST['Submit']) statement). When validating user supplied input, you should NEVER lump tests together. You need to specifically tell the user what was wrong with his input. Here is what that specific validation logic should do - <?php $min_description_length = 5; // the minimum you want to allow $max_description_length = 2000; // set to your database table field size $_POST['premiumuser_description'] = trim($_POST['premiumuser_description']); // condition input if($_POST['premiumuser_description'] == ''){ // empty string $error_message .= "You must enter a Company Description<br>"; $error_message .= "Your Company Description was empty<hr>"; } else { // not empty $desc_length = strlen($_POST['premiumuser_description']); if($desc_length < $min_description_length){ // less than minimum characters (this bed is too small) $error_message .= "You must enter a Company Description<br>"; $error_message .= "Your Company Description was less than $min_description_length characters<hr>"; } if($desc_length > $max_description_length){ // greater than maximum (this bed is too big) $error_message .= "You must enter a Company Description<br>"; $error_message .= "Your Company Description was: " . number_format($desc_length) . "characters. The maximum permitted is: " . number_format($max_description_length) ." characters<hr>"; } if($desc_length >= 5 && $desc_length <= $max_description_length){ // length is okay (this bed is just right) // check for invalid characters // NOTE: This is not the same anti (^) pattern that was used in the starting code $rep = preg_replace("/[a-zA-Z0-9\s\-\'\,\.\_]/", "", $_POST['premiumuser_description']); // strip out allowed characters if($rep != ''){ // some invalid characters found $error_message .= "You must enter a Company Description<br>"; $error_message .= "Your Company Description contained the following invalid characters: <font color='red'>$rep</font><hr>"; // if you want to display the whole message for reference, do that here... } } }
-
Here's some code that will fix what magic_quotes_gpc (get/post/cookie) does, in case you cannot turn the setting off or you want your code to work on any server without needing to mess with a setting that cannot be turned off at runtime - <?php if(get_magic_quotes_gpc()){ $_GET = array_map('stripslashes',$_GET); $_POST = array_map('stripslashes',$_POST); $_COOKIE = array_map('stripslashes',$_COOKIE); }
-
The reason your validation of the description is failing is because magic_quotes_gpc is turned on and it is adding \ escape characters to the data (don't simply add the \ character to the preg_replace pattern because that will create a mess, you must fix what magic_quotes_gpc is doing to the data.) You either need to turn magic_quotes_gpc off or you need to detect if magic_quotes_gpc is on (see get_magic_quotes_gpc ) and remove the \ characters (see stripslashes) from all external data (thanks php.net for more wasted time btw.) If you don't do this, your data will be double escaped (because you are properly escaping it using mysql_real_escape_string before putting it into the query statement) and you will end up with a mess in your database (thanks again php.net for creating a mess.)
-
You can also just use - $query_handle->execute($binds);
-
The links you build for the first, previous, next, last need to also include any other $_GET parameters you are using on the page. See the following post on how to build the links for the page number AND retain any other $_GET parameters - http://www.phpfreaks.com/forums/index.php?topic=348834.msg1646676#msg1646676
-
Here is some re-re-factored logic for the cartaction.php file that moves the php logic to the start of the file (gets it out of your HTML document), shows a method of preventing duplicate data submission in the form processing code, and adds a showCart() function - <?php // moved function definitions to start of file. Typically you would have these in a separate file and include that file. function addToCart() { // if cart does not exist, create an empty cart if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = array(); } // get/condtion the requested item $item = isset($_POST['item']) ? intval($_POST['item']) : false; if($item < 1){ // handle an invalid item number here... die('Invalid Item'); // for demo purposes, just die } // $item is an integer > 0, add to cart/increase quantity in cart if(!isset($_SESSION['cart'][$item])){ // doesn't exist in cart, create it $_SESSION['cart'][$item] = 0; } // increment quantity in cart $_SESSION['cart'][$item]++; header("Location: {$_SERVER['SCRIPT_NAME']}"); // redirect to (GET) the current page to clear $_POST data exit; } function displayCart($connection,$cat_list){ if(!isset($_SESSION['cart']) || empty($_SESSION['cart'])){ echo "Your cart is empty!"; } else { $items = implode(',',array_keys($_SESSION['cart'])); $query = "SELECT * FROM items WHERE id IN($items) ORDER BY name"; $result = mysql_query($query) or die("Query failed: $query<br />Error: " . mysql_error($connection)); echo "<table><tr><th>Category</th><th>Item Name</th><th>Description</th><th>Qty</th><th>Price</th><th>Total</th></tr>"; while($row = mysql_fetch_assoc($result)){ echo "<tr><td>{$cat_list[$row['cat_id']]}</td>"; echo "<td>{$row['name']}</td><td>{$row['description']}</td>"; echo "<td>{$_SESSION['cart'][$row['id']]}</td><td>".sprintf("£%.2f", $row['price']/100)."</td><td>".sprintf("£%.2f", $_SESSION['cart'][$row['id']] * $row['price']/100)."</td></tr>"; } echo "</table>"; } } session_start(); include ('databaseconnect.php'); $connection = connect_db(HOST, USER, PASS); // NOTE: This code assumes that you have defined these constants in the databaseconnect.php file and that you are selecting a database somewhere in your code $cat_list = array(1=>'Copper Range',2=>'Steel Range'); // your list of category id's/descriptions (in a real application this would be retrieved from a database table) // generate navigation menu $nav_menu = ''; foreach($cat_list as $key=>$value){ $nav_menu .= "<li><a href='shop.php?cat=$key'>$value</a></li>\n"; } // get/condition input $submit = isset($_POST["submit"]) ? trim($_POST['submit']) : false; //If form is submitted from a 'Buy' button, add the item to the cart if($submit == "Buy"){addToCart();} ?> <!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" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Your Shopping Cart</title> <link rel="stylesheet" href="styles/site.css" type="text/css" /> </head> <body> <div id="header"> <h1>Your Shopping Cart</h1> </div> <div id="mainblock"> <?php displayCart($connection,$cat_list); ?> </div> <div id="footer"> <form method="post" action="checkout.php"> <p><input type="submit" name="submit" value="Goto Checkout" /></p> </form> <ul class="navlist"> <?php echo $nav_menu; ?> </ul> </div> </body> </html>