
digibucc
Members-
Posts
142 -
Joined
-
Last visited
Everything posted by digibucc
-
sprintf-like for variable-only purposes without printing
digibucc replied to neuroxik's topic in PHP Coding Help
with a foreach... which would defeat your purpose. sprintf follows a format, so that would be more typing and not scalable. i'm surely open to a better way but using foreach to build the query is the best way i've found yet... -
you could do something this for your sql statement, freehand so check it: <?php $sets = count($_POST) / 2; $x = 0; $sql = "INSERT INTO Persons (FirstName,Age) VALUES"; while ($sets > 0){ $sql .= '('. $_POST['FirstName'. $x]. ','. '('. $_POST['Age'. $x]. '),'; $sets --; $x++; } $sql= substr($sql,0,-1); // trim trailing "," ?>
-
post will just send the last box's info with the name. they NEED to have different names in order to be saved as individual values, otherwise you just have multiple input boxes each over-writing the one before it. that is one reason why it was only saving one entry before, the other being your INSERT statement. both need to reflect multiple names or it won't work.
-
yeah that would be why. you're going to need something like this <?php $sql = "INSERT INTO Persons (FirstName,Age) VALUES($_POST['FirstName1'],$_POST['Age1']),($_POST['FirstName2'],$_POST['Age2']),($_POST['FirstName3'],$_POST['Age3'])"; ?> your statement was only inserting one row, this inserts multiple by means of a VALUES statement: INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9); otherwise you would need to do multiple insert statements. you can build a foreach on your _POST to have it build your sql query, that way it's not trying to insert values that don't exist, and it accounts for all values the user created.
-
well all the boxes will have the same name, so they can only have one value instead of: <?php for ($i=1; $i<=$count; $i++) { echo 'Firstname: <input type="text" name="firstname">'; echo 'Age: <input type="text" name="age">'; } ?> try: <?php for ($i=1; $i<=$count; $i++) { echo 'Firstname: <input type="text" name="firstname'. $i. '">'; echo 'Age: <input type="text" name="age'. $i. '">'; } ?> which will give them each a name that adds a number ($i) at the end, so they all have a different name. eg: firstname1, age1 firstname2, age2 firstname3, age3 however, i don't know about that second form being below the first. if it's saving a single result i guess it works but i've not seen that. i don't see how it can send ANY data as it only has the submit button as it's input...
-
for #4, i think it's just that your values need to be enclosed: $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES ('%s','%s')",$naam,$paswoord); instead of $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES (%s, %s)",$naam,$paswoord);
-
then yes, the first two i assume are because that if statement is not validating to true. i would check the statement, maybe like this: <?php if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { //$beheerder = new Beheerder(GetSQLValueString($_POST['TFnaam'], "text"),GetSQLValueString($_POST['confirmpasw'], "text")); echo 'the statement was true!!'; } else { echo 'the statement was FALSE '; } $beheerder->setObj($beheerder); ?> i'll update more... as for the third error, i don't know. i'm not good enough in oop to help with that yet... for #4, i think it's just that your values need to be enclosed: $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES ('%s','%s')",$naam,$paswoord); instead of $insertSQL = sprintf("INSERT INTO beheerder (naam, paswoord) VALUES (%s, %s)",$naam,$paswoord);
-
well for example if ($x == $y) will be true if $x is '0' and y is 0, even though one is a string and one an integer it is still the value - 0 - that counts. === will require the type to be the same as well, to be true. the !, replaces one of the = so !=, is the opposite of ==, and !== is the opposite of ===
-
yeah then you don't want in_array, you want to do the math for it. here's an example from the man page for arithmetic operators: <?php if($n1 % $n2 == 0) echo "n1 is divisible by n2"; ?> because: if the remainder is 0, it divided evenly. so in your code you could do: <?php // set layout vars $x = 1; // set $x echo '<table><tr>'; //start the table, and the row. // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM products"); while($row = mysql_fetch_array($sql)){ $product = $row["product"]; $id =$row["id"]; $price =$row["price"]; if(9 % $x == 0) { // if 9 evenly divides by x echo '<td>'; // start a column } ?> <div id="products"> <form action="" method="POST" name="myform<?php echo $id; ?>" class="myform<?php echo $id; ?>"> <input type="hidden" name="hiddenField" class="hiddenField" value="<?php echo $product; ?>" /> <input type="hidden" name="hiddenField2" class="hiddenField2" value="<?php echo $id; ?>" /> <input type="hidden" name="hiddenField1" class="hiddenField1" value="<?php echo $price; ?>" /> <input type="submit" name="submit" class="submit" value="<?php echo $product; ?>" style="background-color:lightgreen; height:50px; width:100px;"> </form> </div> <?php $end = $x + 1: if(9 % $x !== 0) { // if 9 DOES NOT evenly divide by x echo '<br>'; } else { // else if it DOES, end the column echo '</td>'; } $x ++; // increase x to keep count } // while loop ends //now outside of the loop, $x = $x - 1; //subtract the last $X++ so you know exactly how many buttons were added if(9 % $x !== 0) { // if 9 DOES NOT evenly divide by x - end the column as it wouldn't have ended itself echo '</td>'; } echo '</tr></table>'; // close the row and table ?> past that you'd need to ask for math help, as i'm not well enough versed yet. i am interested in how well the above solution works though.
-
yes though that leaves no room for more than 1 result - i assume that's not possible as you are searching for a member by id or some primary key, but just wanted to point it out. from man page:
-
the first two may be your if statement: if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { beheerder is set if that's true, and beheerder isn't being set - so maybe that's not evaluating to true as it should? edit: actually, looking at this i can't tell which file is which. you have the includes and then code below that, is that the file included or just more code? post each page in it's own code tags, if you would, so we can see what they are
-
do you mean random number of buttons in a column or random order of buttons ? the start numbers must be the end numbers plus 1, it could be coded that way instead of having two arrays. so you would change the end numbers to however many buttons you want in that column, and the start numbers to one after each of those (1 will always be a start number). for random order of buttons, since you are doing it by row you can't do array_rand, you could add "ORDER BY RAND()" to your select statement. if you don't have many rows that's ok, but it's really not the way to do it. you want to try and not use ORDER BY RAND. http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/ this link explains an alternative. or do i not understand you at all?
-
serialize the array to a string, pass it, unserialize it on receipt. http://php.net/manual/en/function.serialize.php http://php.net/manual/en/function.unserialize.php
-
it failed because you ahd single quotes here too <table border='0' changing those to doubles would fix it. however ignace is right, you shouldn't echo it out anyway, my bad for not saying that.
-
yes you can do that, but you'll have to change a lot of double quotes, and they are standard for code like that. i would do this: <?php /* Copyright (C) 2009 Murad <Murawd> Josh L. <Josho192837> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ // Must rewrite // single quote, not double after echo echo ' <fieldset> <legend> Manage User </legend> <table border='0' style="height:100%;width:570px;border:solid 1px #BBB"> <tr class="navtitle" style="height:25px"> <th style="width:auto">Mute A User From Posting</th> </tr> <tr> <td align="center" class="tdbox"> Search for the profile name you wish to mute. </td> </tr> <tr> <td align="center"> <form method="post" action=''> <input type="text" name="name" /> <input type="submit" name="search" value="Search" /> </form> </td> </tr> <tr style="height:25px"> <td class="tdbox"> <span style="float:left"> <a href="?cype=admin">Back To Administration Control Panel</a> </span> <span style="float:right"> <a href="?cype=admin&page=unmuteuser">Unmute User</a> </span> </td> </tr> </table> </fieldset> '; // single quote, not double ending the statement ?> just change the doubles at the beginning and en of the echo statement to singles. just note again, if you need variables in there you would end the single so echo 'the variable ='. $variable. ' and the echo continues...'; you put singles around plain text strings, and close them , with a period after to continue the echo. just review that one line you should get it.
-
you are echoing with double quotes, and then you use double quotes in your html. that effectively ends the echo and leaves html its trying to parse as php see after style, it turns colors? that's because the double quote right there, it thinks you ended the echo statement. you can just use single quotes but you have to end them to insert variables. that's what i do. or escape your double quotes, with \ so: <?php echo "abcd\"e\"fghijk"; ?> or even: <?php echo " <fieldset> <legend> Manage User </legend> <table border='0' style=\"height:100%;width:570px;border:solid 1px #BBB\">"; ?>
-
right but it has to parse that else every time, even though it is not doing ANYTHING. what i do, is comment the else: <?php if($x == $y) { do something(); } // else { x did not == y} ?> you can even comment multiple lines, at least then it's not trying to parse an empty else statement.
-
correct, you're fetching by row so the foreach won't work right, using your while loop you could do this: <?php // set layout vars $x = 1; // set $x $start = array ('1', '10', '19'); //columns will be started at each of these numbers $end = array( '9', '18' ) // set numbers to end columns at echo '<table><tr>'; //start the table, and the row. // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM products"); while($row = mysql_fetch_array($sql)){ $product = $row["product"]; $id =$row["id"]; $price =$row["price"]; if (in_array($x, $start) { // if x equals a number in the start array echo '<td>'; // start a column } ?> <div id="products"> <form action="" method="POST" name="myform<?php echo $id; ?>" class="myform<?php echo $id; ?>"> <input type="hidden" name="hiddenField" class="hiddenField" value="<?php echo $product; ?>" /> <input type="hidden" name="hiddenField2" class="hiddenField2" value="<?php echo $id; ?>" /> <input type="hidden" name="hiddenField1" class="hiddenField1" value="<?php echo $price; ?>" /> <input type="submit" name="submit" class="submit" value="<?php echo $product; ?>" style="background-color:lightgreen; height:50px; width:100px;"> </form> </div> <?php if (!in_array($x, $end)){ // if $x equals anything OTHER than 9, 18, etc - put in a line break echo '<br>'; } else { // else if it DOES equal 9, 18 ,etc end the column echo '</td>'; } $x ++; // increase x to keep count } // while loop ends //now outside of the loop, $x = $x - 1; //subtract the last $X++ so you know exactly how many buttons were added if (!in_array($x, $end)){ // if $x equals anything OTHER than 9, 18, etc - end the column as it wouldn't have ended itself echo '</td>'; } echo '</tr></table>'; // close the row and table ?>
-
ok... foreach is a way of handling arrays in php. an array is a group of elements, so a foreach is saying for EACH element AS $name, do this: so <?php $start = array ('1', '10', '19'); //columns will be started at each of these numbers $end = array( '9', '18' ) // columns will be ended at each of these numbers $x = 1; // set $x echo '<table><tr>'; // start the table foreach ($result as $button) { // i don't know the name of your array of buttons, i called it $result. this takes the array and processes each button according to what we have below. if (in_array($x, $start) { // if x equals a number in the start array echo '<td>'; // start a column } echo $button. '<br>'; // output the button if (in_array( $x, $end) { // if x equals a number in the end array echo '</td>'; // end the column } $X++; // increase $x by one } // do it again. ?> the foreach will repeat until it has process each array element. i forgot an essential part, $x ++; , which tells it to increase x by one. i added it here. i also forgot a bracket. sorry ;(
-
need a little more info on how you need it formatted. do you just want that div duplicated 9 times? how exactly would you like them to be laid out in columns, ie one cell per button, or is a line break fine? it's just formulaic html coding, you just have to find a piece of logic that explains properly what you want to achieve. for example, <?php $start = array ('1', '10', '19'); $end = array( '9', '18' ) $x = 1; echo '<table><tr>'; foreach ($result as $button) { if (in_array($x, $start) { echo '<td>'; } echo $button. '<br>'; if (in_array( $x, $end) { echo '</td>'; } ?> you can do with math, which may be better - but i haven't put much time into php math so i'm not sure how...
-
very cool andy, thank you. i did not know of that
-
http://www.webresourcesdepot.com/11-syntax-highlighters-to-beautify-code-presentation/ https://www.google.com/search?aq=f&q=php+syntax+highlighter+script do you not mean for code?
-
just style it with css as you would normally. you can add styles to H tags, span tags, etc. so for example: echo '<h4 style="color:red;"> text here </h4>'; or echo '<h4 style="color:red;">'. $variable. '</h4>';
-
Pull info from one mysql table based on another's result
digibucc replied to chaosuk's topic in PHP Coding Help
i see your point, perform one query and then just compare the info? i guess that would depend on how many results there will be. if a second query cuts the results that need to be parsed through by a large enough amount, then it's worth it. however you are right, JOINS does have a place here i just wasn't recognizing it