Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=359900.0
-
You can't do that in your query, you will have to do it in the PHP code that processes the query results. Such as while($row = mysql_fetch_assoc($result)) { if(is_file($row['picture'])) { echo $row['picture']; } } Although, you might want to also include a process to remove invalid entries as well.
-
The output from $url is not in a valid XML format. You would have to log/record what WAS returned from that URL to see what the problem is. Could be the page was down or took to long to respond.
-
As I said, that path is a relative path. That means it is relative from the current working directory. The workign directory is different based upon which file is being run. You need the path to be hard coded from the root of the file path, e.g. c:\some_directory\another_directory\. But, if you are on a shared host, I have no idea what it would look like.
-
Inner while loop not working.. it limit to only one row
Psycho replied to thara's topic in PHP Coding Help
Why are you not using the single query? You are welcome to use two queries if you wish, but I will not help in building a poor solution. -
Can you show how you did it? Because THIS is a relative reference $template->SetDir("/template/skin");
-
Inner while loop not working.. it limit to only one row
Psycho replied to thara's topic in PHP Coding Help
Well, something is definitely off. The output you've posted is not in the same order as the query results you posted previously. One thing I do see that is off in the code is the FIELDSET tags. You need to include a closing FIELDSET right before the closing table tag inside the loop. I can't see why the other records are not displaying, but it is difficult to debug these types of errors when I have to do it without the benefit of real data and the database. I'd suggest adding some echo statements into the while loop for debugging purposes to see what is happening. If you want more help then post an export of the relevant DB tables so I can put them into a test db. -
You will have to define the path from the root of the file system. You cannot define a relative path since you are calling it from different places in the file system.
-
Fixed bug and simplified duplicate check code function updateSelects() { var opt1 = document.getElementById('opt1'); var opt2 = document.getElementById('opt2'); var opt3 = document.getElementById('opt3'); var opt1idx = opt1.selectedIndex; var opt2idx = opt2.selectedIndex; var opt3idx = opt3.selectedIndex; //Enable all options var optCount = opt1.options.length; for(optIdx=0; optIdx<optCount; optIdx++) { opt1.options[optIdx].disabled = false; opt2.options[optIdx].disabled = false; opt3.options[optIdx].disabled = false; } //Disable options selected twice opt3.options[opt1idx].disabled = (opt1idx==opt2idx); opt2.options[opt1idx].disabled = (opt1idx==opt3idx); opt1.options[opt2idx].disabled = (opt2idx==opt3idx); return; }
-
Inner while loop not working.. it limit to only one row
Psycho replied to thara's topic in PHP Coding Help
Here is a quick rewrite that *should* do what you need only using one query. I did this without any testing so there may be some minor errors. But, this is the way it should be done require_once ('../../includes/config.inc.php'); require_once( MYSQL1 ); $q = "SELECT c.category_id, c.category_name, s.subject_id, s.subjects FROM category AS c INNER JOIN category_subject AS cs USING(category_id) INNER JOIN subject AS s USING(subject_id) INNER JOIN institute_category AS ic USING (category_id) WHERE ic.institute_id = $instituteId ORDER BY c.category_name, s.subjects"; $result = mysqli_query( $dbc, $q); $catID = false; $max_columns = 2; while ($row = mysqli_fetch_assoc($result, MYSQLI_ASSOC)) { $categoryId = $row['category_id']; $category = $row['category_name']; //Detect change in category if($catID != $row['category_id']) { if($catID!=false) { if($recCount % $max_columns != 0) { //Close previous row echo "</tr>\n"; } //Close previous table echo "</table>\n"; } $catID = $row['category_id']; echo "<fieldset class='alt'>\n"; echo "<legend><span>Category : <em style='color: red;'>{$category}</em></span></legend>\n"; echo "<table class='form_table'><tr>\n"; $recCount = 0; } $recCount++; if($recCount % $max_columns == 1) { echo "<tr>\n"; } $value = "{$row['category_id']}:{$category}:{$row['subject_id']}:{$row['subjects']}"; echo "<td width='50%'>"; echo "<input type='checkbox' name='subject[]' value='{$value}' /> {$row['subjects']}"; echo "</td>\n"; if($recCount % $max_columns == 0) { echo "</tr>\n"; } } if($recCount % $max_columns != 0) { //Close last row echo "</tr>\n"; } //Close last table echo "</table>\n"; echo "</fieldset>"; -
Inner while loop not working.. it limit to only one row
Psycho replied to thara's topic in PHP Coding Help
You should never run queries within loops. You need to figure out how to JOIN the result set so you can run one query. But, I can answer your question pretty simply. Both of your while loops are using (row = So, when the inner loop exits on the first iteration of the outer loop it will also exit the outer loop because $row will equal FALSE -
I'm not going to do all the work for you. This is a help forum, not a do it for me forum. For preventing a form submission until the user selects a map you could add an onSubmit action to the form tag to call a function. That function would check if a value has been selected. If yes, allow the submission. If not, provide an error and prevent the submission. Allowing an option to be selected twice, but not three times is considerably more difficult. It's definitely possible, but I don't have the time to do it the right way so that it is scalable. But, with only three options and wanting to allow the same option to be selected, this hard-coded example will work function updateSelects() { var opt1 = document.getElementById('opt1'); var opt2 = document.getElementById('opt2'); var opt3 = document.getElementById('opt3'); var opt1idx = opt1.selectedIndex; var opt2idx = opt2.selectedIndex; var opt3idx = opt3.selectedIndex; if(opt1idx==opt2idx) { opt3.options[opt1idx].disabled = true; } if(opt1idx==opt3idx) { opt2.options[opt1idx].disabled = true; } if(opt2idx==opt3idx) { opt1.options[opt2idx].disabled = true; } return; }
-
As stated, file_get_contents() is based upon the file system directory, not the web directory. I would suggest using a config file that is loaded on every page that includes a variable to the location of the templates folder.
-
You never stated you were using file_get_contents()! That would use the file system directory structure and not the web directory structure. I don't know why you would be using file_get_contents() instead of an include() anyway.
-
Try defining the path from the root of the site by including a forward slash at the beginning. Otherwise, without it, you are defining it relative from the current working directory. $dir = "/template/skin";
-
how to get the size of a file from directory.
Psycho replied to Sajesh Mohan's topic in PHP Coding Help
It looks like the file path you are providing isn't accessible/valid. Try the following to see if the error is triggered (made some other improvements) function formatbytes($file, $type) { $filesizeBytes = filesize($file); if(!$filesizeBytes) { echo "Unable to access file '{$file}'"; return false; } switch($type) { case "GB": $filesize = $filesizeBytes * pow(.0009765625, 3) // bytes to GB break; case "MB": $filesize = $filesizeBytes * pow(.0009765625, 2) // bytes to MB break; case "KB": default: $type = 'KB'; $filesize = $filesizeBytes * pow(.0009765625, 1) // bytes to KB break; } return round($filesize, 2).' '.$type; } echo formatbytes($file, 'KB'); -
I would definitely implement a JavaScript solution to provide immediate feedback without needing a page refresh or AJAX call. Of course a server-side validation would be required. Here is a quick-and-dirty solution <html> <head> <script type="text/javascript"> function updateSelects() { var opt1 = document.getElementById('opt1'); var opt2 = document.getElementById('opt2'); var opt3 = document.getElementById('opt3'); var opt1idx = opt1.selectedIndex; var opt2idx = opt2.selectedIndex; var opt3idx = opt3.selectedIndex; for(i=1; i<opt1.options.length; i++) { opt1.options[i].disabled = (i==opt2idx || i==opt3idx); opt2.options[i].disabled = (i==opt1idx || i==opt3idx); opt3.options[i].disabled = (i==opt1idx || i==opt2idx); } } </script> </head> <body> Option 1: <select name="opt1" id="opt1" onchange="updateSelects();"> <option value="">- Select -</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="4">Four</option> </select> Option 2: <select name="opt2" id="opt2" onchange="updateSelects();"> <option value="">- Select -</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="4">Four</option> </select> Option 3: <select name="opt3" id="opt3" onchange="updateSelects();"> <option value="">- Select -</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="4">Four</option> </select> </body> </html>
-
Actually, silkfire's solution was closer to what you needed. I mistakenly used the pipe "|" in the character class to signify an OR condition. But, that was incorrect in that context. You should use preg_match_all('#[\d.]+%#', $text, $matches); . . . although the chances of having the pipe symbol next to a percentage would be very rare.
-
querying mysql columns storing comma separeated values
Psycho replied to raymond_feliciano's topic in PHP Coding Help
You should not store comma separated data in field precisely for this reason. It is not too difficult to perform a search on such a column, but it will be difficult to impossible to do anything more advanced such as joining tables. You need to go find a tutorial or two on how to properly set up a normalized database. In this case you should have an associative table for the values where there is one record per value that points back to the parent record in the current table. -
$text = "Bob the builder 5% 17/05/12 and 33.456% on another day, but fdsfd% is not valid"; preg_match_all('#[\d|.]+%#', $text, $matches); print_r($matches); Output Array ( [0] => Array ( [0] => 5% [1] => 33.456% ) )
-
Query results need to be targeted---Please Help
Psycho replied to a6april's topic in PHP Coding Help
Here is an example of how you could replace a large portion of the hard-coded content and make it dynamically created. This ensures that all the elements are being generate3d consistently. If you are hard-coding a lot of content like you have above it is common to have copy/paste errors. And, with so many variables, such an error could go undetected for a long time. <?php $fieldList = array ('Dry', 'Canned', 'Raw', 'FreezeDried', 'Dehydrated', 'Puppy') //Use the aarray to create dynamic HTML $options = ''; $checkboxes = ''; $images = ''; foreach($fieldList as $field) { $options .= "<option value='{$field}'>{$field}</option>"; $checkboxes .= "{$field} <input type='checkbox' name='fields[]' value='{$field}'><br>\n"; $images ,= "<td><img src='images/{$field}.png'></td>\n"; } ?> <h2>Search</h2> <form name="search" method="post" action=""> Seach for: <Select name="find"> <option VALUE="">Select</option> <option VALUE="Y">Yes - My Results Should Contain A Positive Yes</option> <option VALUE="N">No- My Results Should Contain A Positive NO</option> </select> <select name="field"> <option value="">Select</option> <?php echo $options; ?> </select> <p> </p> <?php echo $checkboxes; ?> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <BR><BR> ---------------------- <BR><BR> <table cellpadding=3 border =1> <tr> <td><center><B>BRAND</B></center></td> <?php echo $images; ?> </tr> -
Query results need to be targeted---Please Help
Psycho replied to a6april's topic in PHP Coding Help
I stopped looking at the code when I saw all the "Boolean" fields were set as 30 character VARCHARs. Do NOT use Y/N for fields that should be logical TRUE / FALSE values. Instead use a tinyint type field and use 1/0 for the value. Additionally, since you need to create several sets of HTML output using the same list of values, they should be in an array so you can create that output dynamically. -
As smoseley states htmlspecialchars() is escaping the input to make it safe for HTML output. It will modify the value if certain characters are in it. You need to make a decision if you will run values through htmlspecialchars() before storing them or not. Then you need to do the same thing with values before using them for comparison. You could run values through htmlspecialchars() before storing them and then you can just echo them to the page. However, I would advise against this. I prefer to store values in their "native" state - i.e. no escaqping/sanitizing. If you escape the values for a specific purpose (in this case HTML output) you cannot effectively reverse the process if you need the data for a different output. So, it seems you are doing just that - storing the value without any escaping. So, you need to not escape values if you are going to use them for comparisons in queries. Just make sure you use the appropriate escape functions when outputting the values. But, what you SHOULD be doing is running the value through mysql_real_escape_string() to prevent SQL Injection. Also, it is not common to have a query looking for an exact comparison to a string like this. Typically, you will see LIKE comparisons. So, if the user entered "john" or "doe", it would find the same record. That would be implemented like this $Rep = mysql_real_escape_string($_GET['Rep']); $query = "SELECT RepName FROM Reps WHERE Repname LIKE '%$Rep}%'"; $result = mysql_query($query) or die("Query: $query<br>" . mysql_error()); I would also advise against building your query inside the mysql_query() function. Instead, build the query as a string variable that you can echo to the page when there are errors. It makes debugging much simpler.
-
how to insert data into a table from a dropdown list
Psycho replied to naihr's topic in PHP Coding Help
If you have a table that stores item IDs and their corresponding descriptions then you should ONLY store the item ID as a foreign key in any associated tables. Go ahead and make the "labels" of the select options the concatenated value of the ID and the description, but the "values" should still only be the ID. And, you should ONLY be storing the ID in the incoming_table. That is how a relational database works. -
how to insert data into a table from a dropdown list
Psycho replied to naihr's topic in PHP Coding Help
It seems you have the concepts of "SELECT" of a query and for a form input confused. This makes no sense: $dropdown = "<SELECT CONCAT(itemid,' ' '-',' ', description) AS Item FROM item_table>"; A select list in HTML should look something like this <select name="field_name"> <option value="0">First Option</option> <option value="1">Second Option</option> <option value="2">Third Option</option> </select> Also, you would not typically concatenate the id and description. You would use one for the value of the options and one as the label for the options. Here is an example of the code I would use to build the select input field $query = "SELECT itemid, description FROM item_table"; $result = mysql_query($query) or die(mysql_error()); $item_options = ''; while($row = mysql_fetch_assoc($result)) { $item_options .= "<option value='{$row['itemid']}'>{$row['description']}</option>\n"; } ?> <select name="itemid"> <?php echo $item_options; ?> </select> In your processing page you are trying to retrieve values for both an itemid field and a description field. You only need the itemid - unless the description is for something other than the item.