I'm a complete noob but have been beating my head against the wall for 2 or 3 weeks. I've searched the web more than you can imagine, to get an answer for what seems like simple task, yet everything I find is overkill or underkill, and I haven't been able to adapt the code I find. I hope I can put this to bed, once and for all. My issue is simple, I'm running a 'select' command on MySQL. I am getting the results I want as I have done a var_dump and also ran it in myPHPAdmin. I simply need to display, side by side, two columns from the results of my query, the date and a column called "AIR_PRESSURE". I can iterate through the results and display one column but adding that second column has become beyond rediculous. I'm doing things in two files; index.php, which does the queries and readings.html.php, which formats and displays the results. Here is what I have in index.php. My index.php has an include statement. This is what I have in the index.php: ... while ($row = mysqli_fetch_array($result)) { $sevenday[] = $row['AIR_PRESSURE']; } ... And in my readings.html.php I have this: ... <?php foreach ($sevenday as $sevdaypress): ?> <?php echo htmlspecialchars($sevdaypress, ENT_QUOTES, 'UTF-8')?><br> <?php endforeach; ?> ... This results is printing the the barometric pressure in a column, for the last seven days, as expected. All I want to do is include the date, right next to it. It seems like a no brainer and I'm not even going to go into what I've tried because the number of things I've tried are too numerous to list. I would think I should be able get the date in the same line I get the air pressure, but when I try to add any other columns to that line, besides "AIR_PRESSURE", I get an error. Everything I have found in my searches is for multi-dimensional arrays embedded in other arrays, (overkill) or simple single-column arrays, which I have no problem with. I simply want do be able to display whatever columns I wish, that were in my 'select' statement. I hope I can get some help. I'm exasperated trying to do what should be the simplest of things. Thank you.
The problem here, is that you are doing this in two processes. That means the only way you can get what you want, is by either using a multidimensional array, or doing the while-loop inside the included file. I say this because you probably do not have a unique column (the barometric pressure can be the same for different days, hence can't be used as a key, and, unless you have distinct dates (only one of each), neither can dates). If you DO have unique dates, simply do something like this: index.php while ($row = mysqli_fetch_array($result)) { $sevenday['.$row['DATE'].'] = $row['AIR_PRESSURE']; } PHP: and then in the readings.html.php, do something like this: <?php foreach ($sevenday as $key => $value): echo htmlspecialchars($value, ENT_QUOTES, 'UTF-8')?>,' ',$key<br> endforeach; ?> [php] PHP:
The dates aren't unique in the table but they are in the query result because I'm grouping by date and there is only one barometric pressure per date. I'll give this a try! Thank you! Update: I gave it a try and I get this error. My 'timestamp' column is labeled "CREATED" so I changed 'DATE' to 'CREATED' and I get this error" 2017-05-07 18:52:17: (mod_fastcgi.c.2702) FastCGI-stderr: PHP Parse error: syntax error, unexpected 'CREATED' (T_STRING), expecting ']' in /var/www/html/index.php on line 114 Any ideas?
Try this and let me know. for index file $sevenday=array(); $s=array(); while ($row = mysqli_fetch_array($result)){ $s['ap'] = $row['AIR_PRESSURE']; $s['date'] = $row['date']; $sevenday[]=$s; } And in your read file <?php foreach( $sevenday as $k=>$kk ){ echo 'AirPressure: '. $kk['ap'].' for date: '. $kk['date']. '<br />'; } ?>
Thanks to all of you! I'll give this a try. I truly appreciate your help. BTW, how do you post code blocks on this board? Brian
Wrap 'em in [ code ] [ /code ] (without spaces though). Or click on the Insert icon above to choose what you want to insert.
No need for two arrays while ($row = mysqli_fetch_array($result)){ $sevenday[] = [ 'ap' => $row['AIR_PRESSURE'], 'date' => $row['date']; ]; .... Code (markup): Though I don't think this will fix the OPs last error, we would need to see more code...
I ended up using this with some mild tweaking and it worked! Here's my code: index.php while ($row = mysqli_fetch_array($result)) { $sevenday[$row['DATE(CREATED)']] = $row['AIR_PRESSURE']; PHP: readings.html.php <?php foreach ($sevenday as $key => $value):?> <?php echo htmlspecialchars($value, ENT_QUOTES, 'UTF-8'),' ',$key;?><br> <?php endforeach; ?> PHP: hdewantara was right,there was a typo on the index.php. This is my output: 29.86 2017-05-08 29.78 2017-05-07 29.72 2017-05-06 29.81 2017-05-05 29.90 2017-05-04 29.84 2017-05-03 29.83 2017-05-02 Code (markup): Thanks to all of you! I'll try all of your suggestions to see the difference in the variations. Ya'll are awesome! Just make sure I understand this code, I'm not quite sure what '$sevenday[$row['DATE(CREATED)']] = $row['AIR_PRESSURE'];' is doing. In '?php foreach ($sevenday as $key => $value):?>' I'm assuming it is assigning 'DATE(CREATED)' as the key and the => binds it to the value, (AIR_PRESSURE)? Am I correct?
That is correct - you can assign anything as an array-key, as long as the key is unique (else it will overwrite the already existing key) - that's why I was asking about the dates being unique.
So am correct that it is taking the first variable from the index.php, 'DATE(CREATED)' because it's the first argument in the while loop in the index.php? That's where I'm lost; how does it know which "label" is the key and which is the value? Is it due to the order they appear in the index.php? Sorry to be redundant but I want to make sure I understand exactly what is happening. Also, would you please explain the index.php line, lastday[$row['CREATED']] = $row['AIR_PRESSURE']; Code (markup): I would surely appreciate it because to me it makes one variable equal to the other, cancelling one out. That's where I'm confused. And, thanks again. Trust my, I'm studying. I'm working on some classes on Udemy as well as some ebooks but I'm a bit impatient. Brian PS This is the first forum where I've asked a question and didn't get a snarky answer, like "read the manual". Yes, the manual makes perfect sense, AFTER you know what you're doing, but it's the worst possible learning source. I've hesitate for a month asking a question on a forum because of the way experienced programmers belittle noobs or other who are simply struggling at grasping a concept. Thanks again. Once I have my act together, I hope to be the patient guy answering the question.
Well... no. You're assigning $row['DATE(CREATED)'] as the key in the lastday-array. The value inside the lastday[] square brackets is the key - regardless of what is there - if nothing is there, it just counts from zero (0). No. You're assigning the value ($row['AIR_PRESSURE']) to the lastday-array, with the key $row['DATE(CREATED)']. One, single = means assign a value to a variable. Two (==) means match two variables / values against eachother. Oh, we do that too. However, your question was quite well organised, polite, and you have taken suggestions and examples to heart. That builds goodwill, and as long as someone is willing to learn, there is no ill-will here (most of the time). However, if the last code you posted (the one with multiple <?php / ?>) is the actual code, you should avoid doing multiple start/stop php-statements.