jcbones
Staff Alumni-
Posts
2,653 -
Joined
-
Last visited
-
Days Won
8
Everything posted by jcbones
-
Yes, you can. $result = mysql_query("SELECT * FROM stuff ORDER BY price DESC LIMIT 1"); while($row = mysql_fetch_array($result)) { echo $row['image'] . " " . $row['link']; echo "<br />"; $prices[] = $row['price']; } rsort($prices,SORT_NUMERIC); echo 'Highest price: $' . $prices[0] . '<br />'; echo 'Third Highest Price: $' . $prices[2] . '<br />';
-
Try making the opening php flag a long flag. Paul Ryan did the carnal sin and used short tags in the script. A lot of servers have short tags set to OFF, therefore you will see the PHP code. So, at the top of chat_messages.php find: <? //and change to: <?php And see if that helps.
-
So add error commands to all of your queries. msyql_query('whatever the query is') or die(mysql_error());
-
Basic feedback form with data saved to a database
jcbones replied to spacepoet's topic in PHP Coding Help
Sure: <?php //<- open a php script. $error = NULL; //<- set our errors to NULL. $name = NULL; //<- set name to NULL. $email = NULL; //<- set email to NULL. $comment = NULL; //<-set comment to NULL. if(isset($_POST['submit'])) { //<- check if submit button has been clicked. include('database_connection.php'); //<- include our connection details for database interaction. $name = $_POST['name']; //<- set name input to a variable. $email = $_POST['email']; //<- set email input to a variable. $comment = $_POST['comment']; //<- set textbox to variable. if(empty($name)) { //<- if name is empty. $error .= '--Must have a name. <br />'; //<-this is the error message. } if(empty($email) || preg_match('~^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$~',$email)) { //<- if email is empty, or doesn't follow the expression. $error .= '--Must have a valid email address. <br />'; //<- this is the error message. } if(empty($comment)) { //<- if the textbox is empty. $error .= '--Must leave a comment. <br />'; //<- this is the error. } if($error == NULL) { //<- if there are no error messages. $sql = sprintf("INSERT INTO feedback(name,email,comment) VALUES ('%s','%s','%s')", //<-database structure must be right. mysql_real_escape_string($name), mysql_real_escape_string($email), mysql_real_escape_string($comment)); //<- Build the query. if(mysql_query($sql)) { //<- if the query is accepted by the database. $error .= 'Thank you for your comment!'; //<- this is the message. } else { $error .= 'There was an error in our Database, please Try again!'; //<- if not, this is. } } } echo $error; //<- print errors to the screen. (will also print if the database interaction was successful or not). ?> <form action="" method="post"> <label for="name">Name: <input type="text" name="name" value="<?php echo $name; ?>" /></label><br /> <label for="email">Email: <input type="text" name="email" value="<?php echo $email; ?>" /></label><br /> <label for="comment">Comment: </label><br /> <textarea name="comment" cols="40" rows="10"><?php echo $comment; ?></textarea> <input type="submit" name="submit" value=" Submit " /> </form> -
Making 1 navigation file to control entire site
jcbones replied to spacepoet's topic in PHP Coding Help
If it is automatically adding slashes to your data, then magic_quotes_gpc() is turned on. You should bypass this, and run mysql_real_escape_string(). This second function will properly escape the data, without leaving a slash on the quotes. Something like this should get you started: if(get_magic_quotes_gpc()) { $_POST = array_map('stripslashes',$_POST); $_GET = array_map('stripslashes',$_GET); $_COOKIE = array_map('stripslashes',$_COOKIE); } -
So you want it reset to Tokyo time? OR, you want it set to Chicago time? You aren't real clear on what you want.
-
This line: $Aquery = mysql_query("SELECT * ArtisanAnswer WHERE ART_ID = '$artID' ORDER BY ARQ_ID"); is invalid MySQL syntax, it should read: $Aquery = mysql_query("SELECT * FROM ArtisanAnswer WHERE ART_ID = '$artID' ORDER BY ARQ_ID");
-
Find the total number of individual record in MySQL with php
jcbones replied to phppaper's topic in PHP Coding Help
Well, we have no idea what you database tables look like, but it should be something like: $sql = "SELECT COUNT(id) as count, department FROM table GROUP BY department"; -
There are many formats to join 2 tables together. There is NATURAL, STRAIGHT, INNER, OUTER, LEFT, RIGHT joins. Although many work similar, they all have their own uses, and syntax. Your query should read as. $sql = "SELECT branch.branchpostcode FROM users, branch WHERE users.branch1=branch.id"; //<- Select only the postcode from the branch table. Edit: Left out CROSS join, which is equivalent to INNER and the same as JOIN. You don't use an ON clause with CROSS JOIN but you do with INNER JOIN.
-
Code inserting unwanted blank record into database
jcbones replied to ladykudos's topic in PHP Coding Help
Use mysql_real_escape_string() to help sanitize your data for mysql interaction. -
Don't need a direct answer - just where to look for the error
jcbones replied to boblan66's topic in PHP Coding Help
Do you still have questions on control structures? -
That is called Pagination.
-
Either change the input type to text. <input type='hidden' value='$msg_id' name='replyid'> <-change to a text input or look at the source code and see if the value is set to the proper Id. At the top of the deleteorsend.php put in some de-bugging code; echo '<pre>'; print_r($_POST); echo '</pre>'; Make sure you have all the values you think you do in the post array.
-
I thought protected variables could not be used or modified outside of the parent/child classes that control it.
-
PHP ignores space unless it is inside a string.
-
Getting total for each client between date range.
jcbones replied to w2pc's topic in PHP Coding Help
Running of the same premise. Un-tested. Assuming money is stored as XX.XX and not $XX.XX. $sql = "SELECT a.`client name`, b.total FROM table1 AS a, table2 AS b WHERE a.acct# = b.acct# AND a.status = 'active' AND a.`Starting balance` != a.`ending balance` AND b.`invoice date` BETWEEN '2010-01-01' AND '2010-03-01'"; $result = mysql_query($sql); while($r = mysql_fetch_assoc($result)) { $money[$r['client name']] += $r['total']; } if(is_array($money)) { foreach($money as $name => $total) { echo 'Customer ' . $name . ' owes a total of $' . $total . '<br />' . "\n"; } } If money is stored as $XX.XX //replace $money[$r['client name']] += $r['total']; //with $money[$r['client name']] += substr($r['total'],1); -
Echo out the number of records in a mysql table with PHP?
jcbones replied to Far Cry's topic in PHP Coding Help
//Either $sql = "SELECT * FROM table"; $result = mysql_query($sql); echo mysql_num_rows($result) . ' rows found!'; //or $sql = "SELECT COUNT(*) AS count FROM table"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo $row['count'] . ' rows found!'; I would use the first one, if I'm pulling queries to use, and the second one for counts only. -
Getting total for each client between date range.
jcbones replied to w2pc's topic in PHP Coding Help
Try this: un-tested. $sql = "SELECT a.`client name`, SUM(b.total) as total FROM table1 AS a, table2 AS b WHERE a.acct# = b.acct# AND a.status = 'active' AND a.`Starting balance` != a.`ending balance` AND b.`invoice date` BETWEEN '2010-01-01' AND '2010-03-01'"; -
<?php $sql="INSERT INTO tablename (columnname1, columnname2, columnname3) VALUES ('$variable1','$variable2','$variable3')"; if (mysql_query($sql)) { echo "data added"; } else { echo 'add failed!'; } ?>
-
Yep, I patched that in and forgot to alias the table names. Give us a dump of your table data, and we can get it sorted out.
-
The function wouldn't work because you double nested it with single quotes. <a href='Javascript:checkDelete(234,'Title');'> Will not work as the single quote in the javascript function would close the href in the html element. To fix this, you would have to write your php echo statement with double quotes. echo "<td><a href=\"Javascript:checkDelete(" . $row['mID'] . ",'" . $row['Title'] . "')\">";
-
It works as coded. If you need additional help you will need to specify how you want the calculations to work.
-
Try: <?php include("database.php"); session_start(); $email = $_SESSION['email']; /* now the query */ $query = "SELECT * FROM surveys"; /* now loop thru the results */ while($row=mysql_fetch_array($result)) { echo "<b><a href=\"{$row['url']}{$email}\">{$row['title']}</a></b> - \${$row['pay']} - {$row['info']}<br />"; } /* 0 is ID /* 1 is title /* 2 is info /* 3 is pay /* 4 is url ?>
-
Question about how to track and display time correctly
jcbones replied to JeremyCanada26's topic in PHP Coding Help
Sure, here is a function that sets the timezone according to the gmt. <?php //timezones function. function setTimeZone($gmt) { $times = array( '0' => 'Europe/London', '+1' => 'Europe/Rome', '+2' => 'Asia/Jerusalem', '+3' => 'Europe/Moscow', '+4' => 'Asia/Baku', '+5' => 'Asia/Ashgabat', '+6' => 'Asia/Dhaka', '+7' => 'Asia/Phnom_Penh', '+8' => 'Asia/Hong_Kong', '+9' => 'Asia/Seoul', '+10' => 'Pacific/Port_Moresby', '+11' => 'Pacific/Guadalcanal', '+12' => 'Pacific/Fiji', '-11' => 'Pacific/Midway', '-10' => 'Pacific/Honolulu', '-9' => 'America/Juneau', '-8' => 'America/Los_Angeles', '-7' => 'America/Denver', '-6' => 'America/Chicago', '-5' => 'America/New_York', '-4' => 'America/St_Thomas', '-3' => 'America/Buenos_Aires', '-2' => 'Atlantic/South_Georgia', '-1' => 'Atlantic/Cape_Verde' ); if(!is_numeric($gmt)) { return false; } date_default_timezone_set($times[$gmt]); return true; } //usage of function if(setTimeZone('+10')) { echo date('m-d-Y g:i:s a'); }