jcbones
Staff Alumni-
Posts
2,653 -
Joined
-
Last visited
-
Days Won
8
Everything posted by jcbones
-
Very true, and something that can be implemented easily. Although, I would use: $storage = array_fill(1,31,array());
-
I think I got the idea: $new_r_timer_in = range(0,10); for($y=0; $y<4;$y++) { if(($inn[$y] <= $new_r_timer_in[$i]) && ($outt[$y] <= $new_r_timer_out[$i])) { echo $numm[$y]; } }
-
I would guess: //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); }
-
I can't think of any way to arrange your data to do this inside the while loop that pulls the database values. But, you can store that data to sort it later. My idea is along the lines of: UN-TESTED <?php $sql = "SELECT RainTotal, MONTH(`Date`) as `month`, DAY(`Date`) as `day` FROM wx_daily WHERE YEAR(Date) = '2011' ORDER BY Date Asc"; $result = mysql_query($sql) or trigger_error(mysql_error()); if(mysql_num_rows($result) == 0) { echo 'No data exists!'; exit(); } while($r = mysql_fetch_assoc($result)) { $storage[$r['day']][$r['month']] = $r['RainTotal']; } echo '<table><tr><th>Day</th><th>Jan</th><th>Feb</th><th>Mar</th><th>April</th><th>May</th><th>June</th><th>July</th><th>Aug</th><th>Sep</th><th>Oct</th><th>Nov</th><th>Dec</th></tr>'; foreach($storage as $day => $monthArray) { echo '<tr><td>' . $day . '</td>'; for($i = 1; $i < 13; $i++) { if(array_key_exists($i,$monthArray)) { echo '<td>' . $monthArray[$i] . '</td>'; } else { echo '<td>0</td>'; } } echo '</tr>'; } echo '</table>'; ?>
-
Do you see all of the whitespace before the <?php tag? Remove it.
-
With the code provided, I get a return of 2 (totalPrice). Which would be nrooms (2) x number of nights (1). Nowhere have you multiplied by the price of the room. Here is my test script: <?php $_POST['datein'] = '19 Sep 2011'; $_POST['dateout'] = '20 Sep 2011'; $nrooms = 2; $dateout = explode(' ',$_POST['dateout']); $datein = explode(' ',$_POST['datein']); $newdate = (int)(strtotime($dateout[1] . '-' . $dateout[0] . '-' . $dateout[2] ) - strtotime($datein[1] . '-' . $datein[0] . '-' . $datein[2])) / 86400; echo $newdate .'<br />'; $totalprice = $nrooms * $newdate; echo $totalprice; ?> Output: 1 2
-
I had you a code that put the information in a SQL query. The forums wouldn't work for me yesterday, but here it is anyway. This will give you a well formulated MySQL query. I just pushed every column into a separate database column. <?php $doc = new DOMDocument(); @$doc->loadHTMLFile('weather.html'); $desired_rows = 100; //header and 1st row of data. $table = $doc->getElementsByTagName('table'); $rows = $table->item(1)->getElementsByTagName('tr'); $count = $rows->length; $sql = 'INSERT INTO weather (day,`time`,description,tempature,windDir,windSpeed,windGust,visibility) VALUES '; for($i=2,$start=$i;$i<($start + $desired_rows) && $i < ($count - 1);$i++) { $values = array(); $columns = $rows->item($i)->getElementsByTagName('td'); $columnCount = $columns->length; if($columnCount == { $retainDate = true; } for($n=0;$n<$columnCount;$n++) { $value = $columns->item($n)->nodeValue;//go through the columns. $img = $columns->item($n)->getElementsByTagName('img'); for($ii = 0; $ii < $img->length; $ii++) { $value = $img->item($ii)->getAttribute('title'); } if($retainDate == true && $n == 0) { $date = $value; } elseif($n == 0) { $value = $date . '\',\'' . $value; } $values[] = $value; } $queryValueArray[] = implode('\',\'',$values); $retainDate = false; } $sql .= '(\'' . implode("'),\n('",$queryValueArray) . '\')'; echo "<pre>$sql</pre>"; ?>
-
Try this: <?php echo '<style type="text/css"> table { border-collapse: collapse; } table, th, td { border: 1px solid black; padding: 2px; } </style>'; $doc = new DOMDocument(); @$doc->loadHTMLFile('http://www.metoffice.gov.uk/weather/uk/he/stornoway_forecast_weather.html'); //load the file; $desired_rows = 1; //How many rows you want from the table. $table = $doc->getElementsByTagName('table'); //get our tables out, it should return 2 from the file, we only want the second. $rows = $table->item(1)->getElementsByTagName('tr'); //pull the table rows from the second table (notice we select the second by item(1).) $count = $rows->length; //returns a count of the table rows. echo '<table id="weather"><tr> <th rowspan="2">Date</th> <th rowspan="2">Time</th> <th rowspan="2">Weather</th> <th rowspan="2">Temp</th> <th colspan="3">Wind</th> <th rowspan="2">Visibility</th> </tr> <tr> <th>Dir</th> <th>Speed</th> <th>Gust</th> </tr>'; //mock up of the original table headers. for($i=2,$start=$i;$i<($start + $desired_rows);$i++) { //for loop, goes through the rows. echo '<tr>'; //start row. $columns = $rows->item($i)->getElementsByTagName('td'); //get columns for this row. $columnCount = $columns->length; for($n=0;$n<$columnCount;$n++) { //go through the columns. if($n == 2) { $img = $columns->item($n)->getElementsByTagName('img'); //the 3rd column is an image, so we must get the image title. $value = $img->item(0)->getAttribute('title'); } else { $value = $columns->item($n)->nodeValue; //else we will just take what is in the column. } echo '<td>' . $value . '</td>'; //push the column to the screen. } echo '</tr>'; //end the row. } echo '</table>'; //end the table. ?>
-
You should be with everything that was posted. You were on the right track to begin with. $refresh = "INSERT INTO temp (name, alliance, x coord, y coord ) SELECT player, alliance, x, y FROM players"; //missed a space. INSERT...SELECT MySQL
-
<?php $arr = range(0,4000); $count = count($arr); //loop1 $start = microtime(true); foreach($arr as $key => $value) { $key . $value; } $end = microtime(true); echo 'foreach: ' . number_format(($end - $start),5) . ' seconds!<br />'; //loop2 $start = microtime(true); for($i = 0 ; $i < $count ; ++$i) { $arr[$i]; } $end = microtime(true); echo 'for: ' . number_format(($end - $start),5) . ' seconds!<br />'; ?> results foreach: 0.00312 seconds! for: 0.00065 seconds! Indeed, for is faster!
-
Quick (probably simple) question about coding practice
jcbones replied to Vince889's topic in PHP Coding Help
I think it is laziness. I do the same as you do. It isn't hard to remember that there are 86400 seconds per day, yet I see people all the time do 60*60*24. -
I would suggest using the DOMDocument Object, I think this will be the easiest method for you. Being that the weather doesn't change, I would also write the wanted contents to a file, and only update it once per day. $doc = new DOMDocument(); $doc->loadHTMLFile('http://server.com/some/file"); $widget = $doc->getElementsById('widgetId'); foreach($widget as $element) { echo $element->nodeValue; }
-
The problem is your date format, strtotime() will not accept that particular format, but we can work around that. Try: $dateout = explode(' ',$_SESSION['dateout']); $datein = explode(' ',$_SESSION['datein']); $newdate = (int)(strtotime($dateout[1] . '-' . $dateout[0] . '-' . $dateout[2] ) - strtotime($datein[1] . '-' . $datein[0] . '-' . $datein[2])) / 86400; $totalprice = $nrooms * $newdate; strtotime() will either accept "18 September 2011" or "Sep-18-2011"
-
You need to check your post array: echo '<pre>' . print_r($_POST,true) . '</pre>';
-
You cannot multiply an integer with a date like you are doing. You will have to get the number of nights. $newdate = (int)(strtotime($_SESSION['dateout']) - strtotime($_SESSION['datein'])) / 86400; $totalprice = $nrooms * $newdate;
-
Try something like this: script <?php $user = 1; //link to the user id. if(isset($_GET['read'])) { $id = (int)$_GET['read']; $query = "INSERT INTO messages_read (message_id,user_id) VALUES ('$id','$user')"; mysql_query($query) or trigger_error(mysql_error()); } $sql = "SELECT m.* FROM messages AS m WHERE m.id NOT IN (SELECT message_id FROM messages_read WHERE user_id = '$user')"; $result = mysql_query($sql) or trigger_error(mysql_error()); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { echo '<a href="?read=' . $row['id'] . '">X</a><div>' . $row['message'] . '</div>'; } } else { echo 'There was no messages to show!'; } ?> database tables -- -- Table structure for table `messages_read` -- CREATE TABLE IF NOT EXISTS `messages_read` ( `id` int(11) NOT NULL AUTO_INCREMENT, `message_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `user_id` (`user_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; -- -- Table structure for table `messages` -- CREATE TABLE IF NOT EXISTS `messages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `message` text NOT NULL, `author` int(11) NOT NULL, `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; NOTE that you should be using your current user table, and linking with the user_id from that user table. This is just a working example (un-tested) so you can see what Dan was talking about.
-
Complicated database and PHP ordering and combining
jcbones replied to rockinaway's topic in PHP Coding Help
Is your database normalized? This would be your first step. -
The database has an error on this query: $query = mysql_query("SELECT * FROM members WHERE username ='$username'"); Add in de-bugging: $query = mysql_query("SELECT * FROM members WHERE username ='$username'") or trigger_error(mysql_error());
-
I believe you can just make that a JOIN instead of LEFT JOIN. That way you will not get empty results from the user table. Which could happen if an error occurs on INSERTion.
-
You need to check the timezone settings of MySQL, as it is a separate service than PHP.
-
header error ... i have read the post at the top already
jcbones replied to matleeds's topic in PHP Coding Help
Yes, anything sent to the screen, either plain HTML, or an echo, print, or a function that prints to the page (including display_errors) will send the headers to the browser. -
Need more info. Between the first element of $inn which starts at 7 and ends at 16, and the first element of $outt which starts at 14 and ends at 23. So that would mean you are taking out the last element of $inn. I could help you, but am massively confused.
-
NO, you can only have one ORDER BY clause in a LEFT JOIN query. You could maybe use a sub-query though, but we would need some database structure, data, and your desired results.
-
header error ... i have read the post at the top already
jcbones replied to matleeds's topic in PHP Coding Help
Move all of your processing to the very TOP of your first code snippet. That would mean the included file. -
Running the date_format twice is extra overhead. Just access the column. $getimages=mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y %m %d') as thedate FROM `2011-09` WHERE displaydate <= CURDATE() ORDER by day_id DESC") or trigger_error(mysql_error());