$searchquery = mysql_query("SELECT * FROM regform_admin WHERE status = 'Approved' AND month LIKE '%".$checkmonth."%' AND day LIKE '%".$checkday."%' AND year LIKE '%".$checkyear."%' OR month2 LIKE '%".$checkmonth."%' AND day2 LIKE '%".$checkday."%' AND year2 LIKE '%".$checkyear."%' OR betday LIKE '%".$checkday."%'");
while($fetchres = mysql_fetch_array($searchquery)) {
$vehicles = $fetchres['vehicles'];
echo "<b>$vehicles</b><br>";
}
include "vehicledbconnect.php";
$searchquery2 = mysql_query("SELECT * FROM vehicletbl WHERE vehicle NOT LIKE '%".$vehicles."%'");
while($fetch = mysql_fetch_array($searchquery2)) {
$v = $fetch['vehicle'];
$vehed = $fetch['vehed']; $vehcap = $fetch['vehcap']; $vehyr = $fetch['vehyr'];
$type = $fetch['type'];
echo "<tr>";
echo "<td class=light><input type = 'checkbox' name='chk[]'></td>";
echo "<td class=light>$v</td>";
echo "<td class=light>$type</td>";
echo "<td class=light>$vehyr</td>";
echo "<td class=light>$vehed</td>";
echo "<td class=light>$vehcap</td>";
echo "<td class=light>$vehcolor</td>";
echo "</tr>";
echo "$array";
}
Is it possible to echo all values inside the while statement from $searchquery inside $searchquery2?
For example the values of $searchquery is:
vehicle1
vehicle2
vehicle3
Now I'm going to echo all of it inside $searchquery2, is it possible? I try to echo $vehicles (from $searchquery) inside $searchquery2 but it only echoes the last value (example. vehicle3) because I know it is not inside the while statement of $searchquery.
Is it possible? Thanks.
Build a loop in loop.
Try this:
mysql_query("SELECT * FROM regform_admin WHERE...")
while(...)
{
$vehicles = $fetchres['vehicles'];
echo "<b>$vehicles</b><br>";
mysql_query("SELECT * FROM vehicletbl WHERE vehicle NOT LIKE '%".$vehicles."%'");
while(...)
{
echo ...
}
}
Instead of echoing inside the while loop, try buffering the output to a variable instead:
$ob = '';
while($fetchres = mysql_fetch_array($searchquery))
$ob .= '<b>' . $fetchres['vehicles'] . '</b><br>';
echo($ob);
During your second while loop, $ob is still alive and you can echo it out just as easily. Here I'm storing it as a string, but you can store the values as an array also if you need more granular access to the elements.
Update
If you want to store them into an array, try it this way:
$ob = array();
while($fetchres = mysql_fetch_array($searchquery))
$ob[] = $fetchres['vehicle']; // This pushes the current vehicle onto the end of the array
After this loop, you just have the string for each vehicle in the array, and none of the formatting. You can add that in when you output the elements. Iterate over your array with foreach:
foreach($ob as $k => $v) {
// Inside this loop, $k is the key, or index, and $v is the value.
// You can change the names of these variables by modifying the line above, e.g.
// foreach($ob as $foo => $bar) <-- $foo = key/index, $bar = value
}
Related
I'm trying to identify whether I am looking at the first column in the array.
I haven't tried anything, but googled plenty and cannot find a solution.
while($row = mysqli_fetch_row($sql)) {
echo '<tr>';
foreach ($row as $col) {
if () //NEED CODE HERE
echo "<td><a href = 'https://whatismyipaddress.com/ip-lookup' target = '_blank'>$col</a></td>";
}
echo '</tr>';
}
mysqli_fetch_row fetches "one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero)." So the key of the column is the same as column order.
So you can do this:
while($row = mysqli_fetch_row($sql)) {
echo '<tr>';
foreach ($row as $key => $col) {
if ($key === 0) {
echo "<td><a href = 'https://whatismyipaddress.com/ip-lookup' target = '_blank'>$col</a></td>";
}
}
echo '</tr>';
}
But column is subjected to changes in database structure and SQL query changes. I would personally prefer mysqli_fetch_assoc or mysqli_fetch_object so I can use the column by name instead of order number. Its less error prone. For example,
while($row = mysqli_fetch_assoc($sql)) {
echo '<tr>';
foreach ($row as $key => $col) {
if ($key === 'ip_address') {
echo "<td><a href = 'https://whatismyipaddress.com/ip-lookup' target = '_blank'>$col</a></td>";
}
}
echo '</tr>';
}
Note: $sql here should be a mysqli_query result instead of the actual SQL string.
I keep getting array to string conversion in this code.. please help me
$qty_parse = oci_parse($conn, 'select qty from master_drawing');
oci_execute($qty_parse);
echo "<tr>\n";
foreach ($row as $item)
{
//echo " <td>".($item !== null ? htmlentities($item, ENT_QUOTES):" ")."</td>\n";
echo " <td>".($item);
if (is_numeric($item)){
$quantity = oci_fetch_array($qty_parse, OCI_ASSOC);
echo '/'.$quantity.'<meter value=10 min="2" max="10"></meter>';
}else {
echo ' ';
}
}
Looks like oci_fetch_array() returns array (array in function name should tell you something ;)).
You can use var_dump($quantity); to see what was returned by this function.
I guess that what you need to do is something like this: echo '/'.$quantity['qty'].'<meter value=10 min="2" max="10"></meter>';
First of all, your $row variable is not defined. You can use next solution:
$qty_parse = oci_parse($conn, 'select qty from master_drawing');
oci_execute($qty_parse);
while ($item = oci_fetch_array($qty_parse, OCI_ASSOC))
{
echo " <td>".($item['qty']);
if (is_numeric($item['qty'])){
echo '/'.$item['qty'].'<meter value=10 min="2" max="10"></meter>';
}else {
echo ' ';
}
}
P.S. When oci getting associated array via OCI_ASSOC - your script gets $item variable like:
$item['qty'] = 'value';
If you want to get value from $item as string variable, redefine you variable on loop like:
$item = current($item);
Hi all Im trying to query a database and store the results ($searchResults[]) as an array :
<?php
if(isset($_POST['indexSearchSubmit']))
{
foreach($_POST['industryList'] as $selected)
{
$_POST['industryList'] = $selected;
$locationListResults = $_POST['locationList'];
$results = mysqli_query($con,"SELECT * FROM currentListings
WHERE location = '$locationListResults' AND industry = '$selected'");
$searchResults = array();
while($row = mysqli_fetch_array($results))
{
$searchResults[] = $row['industry'];
$searchResults[] = $row['location'];
$searchResults[] = $row['title'];
$searchResults[] = $row['description'];
}
}
mysqli_close($con);
}
?>
the problem im getting is when I try to echo the result:
<?php
echo $searchResults[0];
?>
its only bringing back 1 result not displaying all the results in the arrray as i want it to.
Could anybody please point out what it is im doing wrong.
Any help would be greatly appreciated
Do like this
<?php
print_r($searchResults); // Prints all array elements
?>
Alternatively, you can make use of a for loop to echo all elements too..
foreach($searchResults as $k=>$v)
{
echo $v;
echo "<br>";
}
Your code puts your data into 1D array. You probably want sth else so instead of this:
$searchResults[] = $row['industry'];
$searchResults[] = $row['location'];
$searchResults[] = $row['title'];
$searchResults[] = $row['description'];
do this:
$tmp = array();
$tmp['industry'] = $row['industry'];
$tmp['location'] = $row['location'];
$tmp['title'] = $row['title'];
$tmp['description'] = $row['description'];
$searchResults[] = $tmp;
or just this (thanks to Barmar):
$searchResults[] = $row;
This way you store your data as 2D array. So every row you obtain remains in one subarray.
To print the row (which is now in 2D array) iterate over subarrayw:
foreach($one_of_searchResult_rows as $k => $v)
{
// do whatever you need with $k and $v
}
I think you want a 2d array. Try this code snippet :
$searchResults = array();
while($row = mysqli_fetch_array($results))
{
array_push($searchResults,$row);
}
This should push each row as an associative array in every cell of your final searchResuts array. You could then access the values like:
echo $searchResults[0]['industry']; //and so on
echo $searchResults[0]; //Should print out the first match/row of the sql result
Lets make it simple :
$serach_result=mysqli_fetch_all ($result,MYSQLI_NUM);
//you should use print_r while trying to print an array
print_r($search_result[0]);
Reference : mysqli_fetch_all
I have a simple news system that i need to change so i can limit the items per page but i don't have anything that would do this, so i need to do this myself.
Basically the function just displays the whole array of the flat files and i would like to limit it to 3 items per page(configurable).
My starting logic was:
$itemspage=pages->current_page;
$itemsperpage=3;
$limitn=$itemperpage*$itemspage;
$itemindex=$limitn-2;
if page was 1 it would display the news from array index 1,2,3
if page was 2 it would display the news from array index 4,5,6
and so on...
i think this logically should work but what if the news in the array can't be split into 3? with this logic this should (at least on the starting index) cause a bug.
The whole code is here:
$list = $this->getNewsList();
$pages = new Paginator;
echo "<table class='newsList'>";
foreach ($list as $value) {
$newsData = file($this->newsDir.DIRECTORY_SEPARATOR.$value);
$newsTitle = $newsData[0];
$submitDate = $newsData[1];
unset ($newsData['0']);
unset ($newsData['1']);
$newsContent = "";
$itemspage=pages->current_page;
$itemperpage=3;
$limitn=$itemperpage*$itemspage;
$itemindex=$limitn - 2;
foreach ($newsData as $value) {
$newsContent .= $value;
}
echo "<tr><th align='left'>$newsTitle</th>
<th class='right'>$submitDate</th></tr>";
echo "<tr><td colspan='2'>".$newsContent."<br/></td></tr>";
}
echo "</table>";
The for hasn't been done yet only the logic behind the split. Could you help me?
I'm not sure how you're building your $newsContent, but maybe your answer is in here:
$list = $this->getNewsList();
$pages = new Paginator;
$itemspage = $pages->current_page;
$itemperpage = 3; // you might want te define this property in the paginator also ($itemperpage = $pages->items_per_page);
$currentPageList = array_slice($list, ($itemspage - 1) * $itemperpage, $itemspage);
echo "<table class='newsList'>";
foreach ($currentPageList as $value) {
$newsData = file($this->newsDir.DIRECTORY_SEPARATOR.$value);
$newsTitle = $newsData[0];
$submitDate = $newsData[1];
unset ($newsData['0']);
unset ($newsData['1']);
$newsContent = "";
$limitn=$itemperpage*$itemspage;
$itemindex=$limitn - 2;
foreach ($newsData as $value) {
$newsContent .= $value;
}
echo "<tr><th align='left'>$newsTitle</th>
<th class='right'>$submitDate</th></tr>";
echo "<tr><td colspan='2'>".$newsContent."<br/></td></tr>";
}
echo "</table>";
Notice the array_slice:
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
I am building a table in which i should put some values inside the <td></td>.
What I have is a $checked value which I convert it in some string like: id1,id2,id3
What I need to do is a loop which permits me to print this values like this. Something like this:
(for i=0, i<length of words, i++) {
echo "<td> .id[i].</td>"
}
My real variable is this:
if(isset($_POST['Submit'])) {
echo "<pre>";
$checked = implode(',', $_POST['checkbox']);
echo $checked;
Thanks!
If $_POST['checkbox'] is already an array then you don't have to implode() it before processing :
foreach ($_POST['checkbox'] as $id) {
echo '<td>' . $id . '</td>';
}