PHP - Moved: What Do You Dread Dealing With Most In Php?
This topic has been moved to Miscellaneous.
http://www.phpfreaks.com/forums/index.php?topic=313673.0 Similar TutorialsThis topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=347191.0 So if I have this code (while working on my local machine )... Code: [Select] for ($i=1; $i<=15; $i++) { if ($timelimit == $i) { ${"time".$i}="selected='selected'"; } } <select name="timelimit"> <option value="1" <?php echo $time1;?>>1</option> <option value="2" <?php echo $time2;?>>2</option> <option value="3" <?php echo $time3;?>>3</option> <option value="4" <?php echo $time4;?>>4</option> <option value="5" <?php echo $time5;?>>5</option> <option value="6" <?php echo $time6;?>>6</option> <option value="7" <?php echo $time7;?>>7</option> <option value="8" <?php echo $time8;?>>8</option> <option value="9" <?php echo $time9;?>>9</option> <option value="10" <?php echo $time10;?>>10</option> <option value="11" <?php echo $time11;?>>11</option> <option value="12" <?php echo $time12;?>>12</option> <option value="13" <?php echo $time13;?>>13</option> <option value="14" <?php echo $time14;?>>14</option> <option value="15" <?php echo $time15;?>>15</option> </select> When I view the select menu in the browser, all but one of the options says "Notice: Undefined variable: time1 in C:\wamp\www\yyyyyy\create.php on line 112". It doesn't say this if I publish everything to the internet. So the question is, should I put code in to eliminate those Notices, or does it not matter. In other words, if I add this snippet below to declare all the other variables, it eliminates all the Notices. But is this necessary/good practice to do?.. Code: [Select] for ($i=1; $i<=15; $i++) { ${"time".$i}=""; } Hi I found a tutorial on dealing with databases he http://www.phpfreaks.com/tutorial/php-basic-database-handling It's great, except that I need it to do more than just a name; I need first, last, dob and email. I have it figured out how to order, insert and delete, but can someone help me figure out how to do the updating? The tutorial creates an array called cname and loops through it to do the update, but when I add more fields, it only holds the last fileds (emails in this case) I just need to be able to update all the fields for each user instead of just the name. Sounds easy but I've gotten nowhere Everything I have tried is posted below. Any help? Thanks Code: [Select] // INSERT: if we have a name to add... if($_POST['fname']) { // little bit of cleaning... $fname = mysql_real_escape_string($_POST['fname']); $lname = mysql_real_escape_string($_POST['lname']); $dob = mysql_real_escape_string($_POST['dob']); $email = mysql_real_escape_string($_POST['email']); // check for previous entry $sql_check = "SELECT id FROM test WHERE fname='$fname' AND lname='$lname'"; $res_check = mysql_query($sql_check, $conn) or trigger_error("SQL", E_USER_ERROR); if (mysql_num_rows($res_check) == 1) { while ($row = mysql_fetch_assoc($res_check)) { $id = $row['id']; } // update $sql = "UPDATE test SET Current=1 WHERE id='$id'"; $res = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } else { // insert new name into table $sql = "INSERT INTO test (id, fname, lname, dob, email) VALUES ('', '$fname', '$lname', '$dob', '$email')"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } } // end if // UPDATE: if we have name(s) to change... if($_POST['udata']) { /* // for each name to change... foreach ($_POST['udata'] as $input => $v) { echo "$input = $v<br />"; $sql = "UPDATE test SET "; foreach ($v as $id => $value) { echo "$id = $value<br />"; } $sql .= "WHERE ..."; } echo "<hr />$sql<hr />"; print_r($_POST['udata']); */ for ($i = 0; $i < count($_POST['udata']['fname']); $i++) { $fname = $_POST['udata']['fname'][$i]; echo "$i = $fname<br />"; } /* foreach($_POST['cname'] as $cid => $cname) { // little bit of cleaning... $id = mysql_real_escape_string($cid); $fname = mysql_real_escape_string($cname); $lname = mysql_real_escape_string($cname); $dob = mysql_real_escape_string($cname); $email = mysql_real_escape_string($cname); // update name in the table $sql = "UPDATE test SET fname = '$fname', lname='$lname', dob='$dob', email='$email' WHERE id = '$id'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } // end foreach */ } // end if // DELETE: if we have a name to delete... if($_GET['id']) { // little bit of cleaning... $id = mysql_real_escape_string($_GET['id']); // delete name from table $sql = "UPDATE test SET Current=0 WHERE id= '$id'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } // end if // ORDERBY: if one of the links was clicked.. if ($_GET['orderby']) { // make an aray of allowed names $allowed = array('id','fname', 'lname', 'dob', 'email'); // bit of cleaning... $order = mysql_real_escape_string($_GET['orderby']); // is it a valid column name? yes: use it. no: default to 'id' $order = (in_array($order, $allowed))? $order : "id"; // if no link clicked, default to 'id' } else { $order = "id"; } // end else // SELECT: get the list of names from database $sql = "SELECT id, fname, lname, dob, email FROM test WHERE Current=1 ORDER BY $order"; //$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $result = mysql_query($sql, $conn) or die(mysql_error()); /**** end deal with the database ****/ /**** list everything out ****/ // list columns echo "<form action = '{$_SERVER['PHP_SELF']}' method = 'post'>\r\n"; echo "<table border = '1'>\r\n"; echo "<tr>\r\n"; echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=id'>id</a></td>\r\n"; echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=fname'>fname</a></td>\r\n"; echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=lname'>lname</a></td>\r\n"; echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=dob'>dob</a></td>\r\n"; echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=email'>email</a></td>\r\n"; echo "<td>delete</td>\r\n"; echo "</tr>\r\n"; // loop through list of names while ($list = mysql_fetch_assoc($result)) { echo "<tr>\r\n"; echo "<td>{$list['id']}</td>\r\n"; echo "<td><input type = 'text' name = 'udata[fname][{$list['id']}]' value = '{$list['fname']}' />\r\n"; echo "<td><input type = 'text' name = 'udata[lname][{$list['id']}]' value = '{$list['lname']}' />\r\n"; echo "<td><input type = 'text' name = 'udata[dob][{$list['id']}]' value = '{$list['dob']}' />\r\n"; echo "<td><input type = 'text' name = 'udata[email][{$list['id']}]' value = '{$list['email']}' />\r\n"; echo "<td><a href = '{$_SERVER['PHP_SELF']}?id={$list['id']}'>delete</a></td>\r\n"; echo "</tr>\r\n"; } // end while // list input box for adding new entry echo "<tr>"; echo "<td bgcolor = 'gray'></td>\r\n"; echo "<td><input type = 'text' name = 'fname' /></td>\r\n"; echo "<td><input type = 'text' name = 'lname' /></td>\r\n"; echo "<td><input type = 'text' name = 'dob' /></td>\r\n"; echo "<td><input type = 'text' name = 'email' /></td>\r\n"; echo "<td bgcolor = 'gray'></td>\r\n"; echo "</tr><tr>\r\n"; echo "<td></td>\r\n"; echo "<td align = 'center'><input type = 'submit' value = 'submit' /></td>\r\n"; echo "<td></td>\r\n"; echo "</tr></table></form>\r\n"; /**** end list everything out ****/ I am using file_get_contents to get file off the internet for further processing but somtetimes there is error fetching that file off the web in which case it would be nice to try to reload it again but I am not sure what would be best way to do it? How can I jump back on error? I guess there would have to be some jump to label condition, but how do I implement this? <?php if ($beyondportal % 26 == 0 || $beyondportal == 0){ ?> MY CONTENT <?php } ; ?> This code compares my variable $beyondportal and if it is a multiple of 26 counting up from 0, it prints my content. This is great but sometimes the number comes up a negative that is a multiple counting down from 0 like -26, -52, etc.. if I could do the opposite of a modulus I would think it would work but I'm not sure how to in php. any suggestions? Hi All, I've a loop which creates an array as follows: $product_table[] = ['SKU' => $sku, 'Label' => $attribute_name, 'Value' => $term_obj->name ]; I'm then grouping the data by SKU code: #group the products by SKU $group_products = array(); foreach ($product_table as $element) : $group_products[$element['SKU']][] = $element; endforeach; Finally, I output the data: #output the data foreach ($group_products as $itemName => $rows) : echo '<tr>'; #echo '<td>', $element['SKU'], '</td>'; $i=0; foreach ($rows as $row) : $i++; #echo '<td>'. $row["SKU"]. '</td><td>'. $row["Label"]. '</td><td>'. $row["Value"]. '</td>'; if ($i == 1): echo '<td>'. $row["SKU"]. '</td><td>'. $row["Value"]. '</td>'; else: echo '<td>'. $row["Value"]. '</td>'; endif; #echo '<td>'. $row["Value"]. '</td>'; endforeach; echo '</tr>'; endforeach; ?> And looks like: Product code System Pack Quantity XT1CWH System 1 1 x 3m XT2CWH System 2 1 x 3m XT3CWH System 3 1 x 3m
This works perfectly fine. However, some products share the same SKU and therefore it causes an issue, like the below: Product code System Pack Quantity XT1CLWH System 1 8 x 3m System 2 8 x 3m System 3 8 x 3m Is there a way I can avoid this, so if perhaps creates the new row but shows the same SKU code? Many thanks I have a mysql table like so ID OrderID Name Image CatalogID 1 1 test pic1.jpg 1 2 2 test2 pic2.jpg 1 3 1 test3 pic3.jpg 2 4 1 test4 pic4.jpg 3 5 1 test5 pic5.jpg 4 6 2 test6 pic6.jpg 4 7 3 test7 pic7.jpg 4 etc etc (1) I want to use up and down buttons so that the OrderID can be modified to reflect the users order preference, how would I code it so it changes all the OrderID values in the table as necessary to produce the correct order using the buttons, the only affected items each time would be the ones in the same CatalogID group. (2) How do I re number the OrderID when an item is deleted so that it moves all the items below it in the same CatalogID group up one value to still reflect the correct order and not skip OrderID values. How can I check if the text entered in a textarea field equals the text I have stored in a mysql table? Code: [Select] $submitted_description = trim(mysqli_real_escape_string($db,$_POST['description'])); $query = "SELECT description FROM pets"; $result = $db->query($query); $row = $result->fetch_assoc(); $sql_description = $row['description']; Here's what they echo: Quote submitted_description: Testing...\r\n\r\n1\r\n\r\n2\r\n\r\n3 sql_description: Testing... 1 2 3 I tried adding the following to get rid of \r\n from $submitted_description but they both still don't equal but echo the same text. Code: [Select] $submitted_description = str_replace(array("\r","\n",'\r','\n'),' ', $submitted_description); They following code only matches when no spaces are entered in the textarea field. Code: [Select] if($submitted_description == $sql_description) { echo "Match!"; } else { echo "Don't Match!"; } How can this be done? I also tried adding nl2br() to both variables but that didn't work either. I am using Guzzle as a HTTP client, and the following script results in the following error: $response = $this->httpClient->request('GET', "http://$this->host:$this->port/query", ['query' => $data]); $body = $response->getBody(); $rs=json_decode($body, true); Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 136956008 bytes) in /var/www/vendor/guzzlehttp/psr7/src/Stream.php on line 80 What are the work arounds? Instead of trying to convert it into an array all at once, how can I do so in pieces? I know the expected format so it seems I will need to read just the appropriate amount of bytes and then decode parts at a time. Seems like a pain. Are there any classes designed to do so, or can any of the following Guzzle built in methods be used? Thanks Guzzle Response methods: __construct getStatusCode getReasonPhrase withStatus getProtocolVersion withProtocolVersion getHeaders hasHeader getHeader getHeaderLine withHeader withAddedHeader withoutHeader getBody withBodyGuzzle Body methods: __construct __destruct __toString getContents close detach getSize isReadable isWritable isSeekable eof tell rewind seek read write getMetadataI'm dealing with data that WordPress creates for Users when they register and subscribe, and to use it for other purposes, I have to wrap my head around serialized data. It's not sinking in well. The below code works well for what I wanted at the time. It takes into consideration where the Subscriber lives (divided up into five areas) and counts how many I have. That is noted by wp_s2member_custom_fields and ANY s2member_level. There are three levels, and I need to actually note how many there are at each Member level. There is another line of data for each user_id meta_key = wp_capabilities meta_value = a:1:{s:15:"s2member_level2";s:1:"1";} Code: [Select] $custom = 'SELECT * FROM wp_usermeta WHERE meta_key = "wp_s2member_custom_fields" AND user_id IN (SELECT user_id FROM wp_usermeta WHERE meta_value LIKE "%s2member_level%")'; $c_results = mysql_query($custom); $region = array(); while($line = mysql_fetch_assoc($c_results)) { $meta_value = unserialize($line['meta_value']); $region[$meta_value['county']]++; }; foreach ($region as $key => $value) { echo "Region $key: $value members<br>"; } Hi I am new to PHP / mySQL although not to programming and am trying to work out how to deal with the following situation: I have a form for a user to add a book to a database, along with the genres that the book belongs to. Each book can have a number of genres . There are 3 tables, books, genres and bg_xref in the following formats table: books fields: id, title, author, date published table: genres fields: id, genre table: bg_xref fields: id, book_id, genre_id I am now trying to build the form that allows the user to update an existing entry for a book. the genres are selectable from a multi-selection list which I am populating from all the possible values in the genres table. Currently I have the following database selections: mysql_select_db($database_conn, $conn1); $query_rs_getBooks = "SELECT books.id, books.title, books.author, bg_xref.genre_id FROM books LEFT JOIN bg_xref ON books.id = bg_xref.book_id"; $rs_getBooks = mysql_query($query_rs_getBooks, $conn1) or die(mysql_error()); $row_rs_getBooks = mysql_fetch_assoc($rs_getBooks); mysql_select_db($database_conn, $conn1); $query_rs_getGenre = "SELECT * FROM book_genres ORDER BY genre ASC"; $rs_getGenre = mysql_query($query_rs_getGenre, $conn1) or die(mysql_error()); $row_rs_getGenre = mysql_fetch_assoc($rs_getGenre); I have then created a form that the user uses to update the book, but am trying to work out how to prepopulate the selection list based on teh results of the join e.g. <form method="POST" action="<?php echo $editFormAction; ?>" name="update_book"> <fieldset class="full"> <legend>Enter book details below</legend> <table> <tr><td>Title: </td><td><input type="text" size="50" value="<?php echo htmlentities($row_rs_getBooks['title'], ENT_COMPAT, 'iso-8859-1'); ?>" name="title"></td></tr> <tr><td>Author: </td><td><input type="text" size="30" value="<?php echo htmlentities($row_rs_getBooks['author'], ENT_COMPAT, 'iso-8859-1'); ?>" name="author_surname"></td></tr> <tr><td>Genre(s)</td><td><select name="genre" multiple size="4"> <?php do { ?> <?php if (($row_rs_getBooks['genre_id']) != $row_rs_getGenre['id']) { ?> <option value="<?php echo $row_rs_getGenre['id']?>"><?php echo $row_rs_getGenre['description']?></option> <?php } else { ?> <option value="<?php echo $row_rs_getGenre['id']?>" selected><?php echo $row_rs_getGenre['description']?></option> <?php } } while ($row_rs_getGenre = mysql_fetch_assoc($rs_getGenre)); $rows = mysql_num_rows($rs_getGenre); if($rows > 0) { mysql_data_seek($rs_getGenre, 0); $row_rs_getGenre = mysql_fetch_assoc($rs_getGenre); } ?> </select></td></tr> </table> </fieldset> <input type="hidden" name="id" value="<?php echo $row_rs_getBooks['id']; ?>"> <input type="submit" value="Update book details"> <input type="hidden" name="MM_update" value="update_book"> </form> Book id = 1 is classified against genres 1 and 5, so my SQL query returns 2 rows: book.id = 1 + bg_xref.genre_id = 1 book.id = 1 + bg_xref.genre_id = 5 At the moment the form is generated for the first row of the results set with <tr><td>Genre(s)</td><td><select name="genre" multiple size="4"> <option value="5">Humour</option> <option value="4">Non Fiction</option> <option value="2">Novel</option> <option value="3">Picture Books</option> <option value="1" selected>Poetry</option> with a second entry in the results set for book 1 and genre 5. What I need to end up with is a second occurence of book 1 with both genres 1 and 5 set in the form. Thanks Stuart MySQL returns an error in the form of a number, a state, and a message. Without parsing the message you will not be able to determine what column is duplicated.While parsing the error code, I have also notice that, if you have multiple unique fields as duplicates, only the first duplicate encountered will be returned in the message. This is not very helpful to the end user.
Is there any way to parse the returned error code to reflect all duplicate fields, please see sample code below?
$error=array(); $sql = 'INSERT INTO staff(username, email, phone) VALUES (?, ?, ?)'; $stmt = $conn->stmt_init(); $stmt = $conn->prepare($sql); // bind parameters and insert the details into the database $stmt->bind_param('sss', $username, $email, $phone); $stmt->execute(); if ($stmt->errno == 1062) { $errors[] = "One of the fields is already in use."; } Man, I'm having a rough week with questions--if you're following at home. Basically matching a Coach's first and last name to another table, which if matched will yield information about that coach's team. Code: [Select] // or get user by username $current_user = wp_get_current_user(); $current_first = $current_user->user_firstname; $current_last = $current_user->user_lastname; $current_id = $current_user->ID; echo '<div class="contact_form">'; echo $current_first . ' ' . $current_last .' :: '; $query = "SELECT * FROM schools WHERE coachFirst='".$current_first."' AND coachLast='".$current_last."'"; $result = mysql_query ($query); if(!$result) { echo 'There is not a match for your name.'; } else { while($school= mysql_fetch_array($result)) { echo $school['school'] . ' ' . $school['id']; include(ABSPATH ."wp-content/plugins/my-team/form.php"); } echo '</div>'; echo '<div class="roster">'; include(ABSPATH ."wp-content/plugins/my-team/roster.php"); echo '</div>'; } This topic has been moved to PHP Installation & Configuration. http://www.phpfreaks.com/forums/index.php?topic=319595.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=315910.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=342987.0 This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=353027.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=342919.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=319767.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=313579.0 |