I have a procedure that outputs a table with 4 columns and 2 rows. I want data on the 3rd column and second row. The code here gives me the row data for the different columns as i change them below but it outputs the first and second row data for the 3rd column like this: row1datarow2data. Its both row values and I only want the 2nd row. Can someone please help me fix this.
Thanks.
<?php
$result=odbc_exec($conn, $query);
//fetch tha data from the database
while(odbc_fetch_row($result))
{
echo "<tr>";
echo "<td>";
//if i change the 3 under here to a 2 I get the other two row values for the 2nd,column.
echo odbc_result($result,3);
?>
The code as you have shown it is going to loop through all the records and echo the column value specified in your call to odbc_result().
If you do not want to echo the value from the first row, your code needs to detect which row of data is currently being fetched and only echo the column value for the desired row.
For example, in pseudo code:
// include inside of fetch loop
if (current_row is row2){
echo 3rd_column_data_value;
}
Related
I have a bootstrap table which has a form in it, with each row containing a column where the value can be changed from a drop-down box. On clicking the 'Save changes' button, all the rows will be updated with the new values.
The form/table works as intended in normal cases. But if I use the search functionality of the bootstrap table to filter out a few of the rows and then try to update rows with the values, the wrong rows are getting affected.
So from the example in the above image, if I filter out to view just the second row like in the picture below, then the changes, or the 'Update' query is executed on the actual first row, that is to the row which had 'Bob' as the 'technician'.
I'd like to know how to solve this issue.
Here is the relevant code:
foreach($tickets as $tickets)
{
$users = $app['database']->selectAll('users');
echo ("<input type='text' style = 'display:none' value = '$tickets->id' name = 'ticketid[]'>");
echo "<td class = '$technician->color'>$technician->name</td>";
echo "<td>";
echo '<select name = "user[]" id="user" class="form-control">';
echo "<option value = $technician->id>$technician->name</option>";
foreach($users as $users)
{
echo "<option value = $users->id>$users->name</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
}
For the database part, I am calling a function transferTask which accepts first the table name, then two arrays, one array containing updated usernames from the dropdown fields and one containing corresponding ids.
transferTask('tickets', $user[$i], $ticketid[$i])
The above function is executed for each row in the table. I think it's an issue wit the name array being passed from the form to this function but I'm not sure. Help is appreciated!
A PHP page must show the content of a database's table. In another table, there is the value I will use to print the row accordingly and show as a child.
$result = pg_query($conn, "SELECT *,'SENT' as direction FROM table1 WHERE identification LIKE '%$identification%' AND expedition LIKE '$expedition' order by date DESC LIMIT '$limit' ");
This, inside a WHILE, will print this kind of table:
while ($row = pg_fetch_row($result)) {
$sql2 = pg_query($conn, " SELECT quantity FROM expedition WHERE identification='$row[2]' ");
$row2 = pg_fetch_row($sql2);
$color="white";
if ($row2[0] == "-1") { $color="red";}
if ($row2[0] == "1") { $color="green";}
if ($row2[0] == "0") { $color="grey";}
echo "<tr bgcolor=\"$color\">
<td><button class=\"btn btn-info\" type=\"button\" data-toggle=\"collapse\" data-target=\"#$row[2]\">+</button></td>
<td>row[0]</td>
<td>row[1]</td>
<td>row[2]</td>
<td>row[3]</td>
<td>row[4]</td>
<td>...and more</td>
</tr>";
echo "<tr><td colspan=9><div style=\"background-color:$color\" id=\"$row[2]\" class=\"collapse\">$row2[0]</div></td></tr> ";
I am using bootstrap to add a button that, once clicked, will open a hidden row with the content of $sql2. Query that I will print straight after the </tr>.
Notice the 1st query as row[] and the second query as row2[].
The problem:
If the second query contains more than 1 result and in all cases identification is not a PK (therefore there could be more), I am not able to print for each row the colour of its childs.
I need to extend the second query to manage a WHILE loop, in order to extract, through identification, all the values of its parent.
IF, sql2 contains -1 or 1 or 0, the parent and itself must be painted accordingly.
IF, sql2 contains different values, both of them must be painted red (as error).
The obvious choice would be to move the SQL2 at the end of the first WHILE, in this case I can have SQL2 with all the records I want.
just like using arrays or the FOR structure.
Stays though the fact that moving the code would not allow me to print all the rows, because html is wrote already.
In this case I believe I'd need Jquery to highlight the values once they are written already.
I do not know a thing of Jquery and I'd appreciate any suggestion or help.
Attached a image to show what I achieved, the green bar only contain 1 child as the code above. Forgive the ugly interface!
I have two while loops one is to loop through a chat log to retrieve date , username , message and the other while loop is to retrieve icons from a separate table this has two columns chars and image (image-name.*) I can display everything from the table chat but cannot seem to get my second while loop that contains the str_replace to loop through each row in the emo_cons table that may contain values , It only displays the last row in the table if I add a new chars value and image-name.* it shows me back that last one
// select all from table emo_cons that contain character values and image location
$emocom = mysqli_query($con,"SELECT * FROM `emo_cons`");
// fetch rows from query
while($emo_row= mysqli_fetch_assoc($emocom))
{
// assign chars row characters that represent the coresponding image
$chars = $emo_row['chars'];
// assign imagetag to row images that represent the coresponding characters
$imagetag = "<img width='50' class=image height='50' src='chaticons/".$emo_row['image']."' />";
echo " ";
echo "<br>";
} // end while emo_car check
$chat_log = mysqli_query($con,"SELECT * FROM chat ORDER BY id DESC");
// fetch all rows that contain characters and image locations
while($chat_row = $chat_log ->fetch_array()){ // i WANT THIS WHILE LOOP TO OUTPUT ALL ROWS THAT CONTAIN CHARS & IMAGES-LOCATIONS
// name of user in chat log
$username = $chat_row['username']; // this line is for usrer
echo "<br>";
// timestamp
echo $chat_row['date'];
echo "<br>"; // line break
//users profiler avatar image from chat_row while
echo "<img width='50' class=image height='50' src='avatars/".$chat_row['imagelocation']."' alt='Profile Pic'>";
echo " "; // space
// THIS LINE ONLY OUTPUTS THE LAST ROW OF AN ICON IN THE TABLE
// I would like my while to output all rows that contain str_replace chars TO imagetag from its table AND HAVE IT THEN PARSE TO THE MESSAGE [msg]
echo $username. " Says ".$new_str = str_replace($chars,$imagetag,$chat_row['msg']);
echo "<br>";
} // end while chat_log
echo '</div>'; // end div
?>
I need help understanding what I doing wrong I have tried moving my loops around to see if I had incorrectly structured them but had no luck so far
I just want to be able to display the chat message and if the message contains a character to replace that with an icon and do this always and NOT just for last row in my table .
I understand I may not get any help on this but its worth a mention maybe someone can see my errors . Thanks in advance .
UPDATE WORKING
Make following changes in your emo_cons while loop
<?php
// select all from table emo_cons that contain character values and image location
$emocom = mysqli_query($con,"SELECT * FROM `emo_cons`");
// fetch rows from query
$chars = array();
$imagetag = array();
while($emo_row= mysqli_fetch_assoc($emocom))
{
// assign chars row characters that represent the coresponding image
array_push($chars, $emo_row['chars']);
// assign imagetag to row images that represent the coresponding characters
array_push($chars, "<img width='50' class=image height='50' src='chaticons/".$emo_row['image']."' />");
echo " ";
echo "<br>";
} // end while emo_car check
?>
Let me know if it works for you.
Why your code was not working
Both the variables $chars and $imagetag were inside while loop and with every iteration values were overwriting. Hence the last value was stored in both the variables.
So to fix this, I created 2 arrays with same name and all the required values pushed into the arrays, which we later used in the str_replace function.
I've written a PHP script that can populate a table in a particular way so that multiple events (or no events) can be put in one square in an HTML - similar to the layout a calendar would have. But, there's a problem, the while statement I created to fill in squares in the table when there is no data doesn't detect when there is data, and fills the entire table with empty squares. This is what the output looks like (The page is styled using Bootstrap 3). From the mysql data I have provided, these events should be in the square at {Period 1, Monday}.
Here is my data in a mysql database; mysql data
Here is a snippet of the part of the page related to this table;
<?php
$query = "SELECT * FROM configtimetabletwo WHERE term = ".$term." AND week = ".$week." ORDER BY period, day LIMIT 100;";
$results = mysqli_query($conn, $query);
$pp=1; //The current y value of the table
$pd=0; //The current x value of the table
echo '<tr><td>';
while($row = mysqli_fetch_row($results)) {
while((pd!=$row[3] or $pp!=$row[4]) and $pp<6){ //This while statement fills in empty squares and numbers each row.
if($pd==0) {
echo $pp."</td><td>";
$pd++;
}
elseif($pd<5){
echo "</td><td>";
$pd++;
}
else {
echo "</td></tr><tr><td>";
$pd=0;
$pp++;
}
}
echo '<a href="?edit='.$row[0].'" class="label label-default">';
echo $row[5].' '.$row[6].' - '.$row[7]."</a><br>";
}
echo "</td></tr></table>"
?>
I haven't been able to figure out why this happens so far, thanks in advance to anyone who has any idea what's going on.
In the comments below my question, pavlovich pointed out the error. In this case, it was simply an issue of forgetting to use a $ to reference a variable. It would seem that this doesn't throw an error in a while statement like it would elsewhere.
I have 2 questions. One is if there is any error in this code.
The 2nd question I want to ask is how do you know which <li> item is selected. Right now, the code performs a search in mySQL for all rows that matches city, language and level and returns the results in a list item.
I want it so that when the user clicks on anyone of the list items, it will goes into another page displaying a more detail description by querying the selected list item.
I have a guess, which is for step 2, I also grab the ID (primary key) for each row and somehow keep that stored within the list but not echo.. Would I need to wrap <a> in <form action="XX.php" method="get">?
<?php
//1. Define variables
$find_language = $_GET['find_language'];
$find_level = $_GET['find_level'];
$find_city = $_GET['find_city'];
//2. Perform database query
$results = mysql_query("
SELECT name, city, language, level, language_learn, learn_level FROM user
WHERE city='{$find_city}' && language='{$find_language}' && level='{$find_level}'", $connection)
or die("Database query failed: ". mysql_error());
//3. Use returned data
while ($row = mysql_fetch_array($results)){
echo "<li>";
echo "<a href=\"#result_detail\" data-transition=\"flow\">";
echo "<h3>".$row["name"]."</h3>";
echo "<p>Lives in ".$row["city"]."</p>";
echo "<p>Knows ".$row["level"]." ".$row["language"]."</p>";
echo "<p>Wants to learn ".$row["learn_level"]." ".$row["language_learn"]."</p>";
echo "</a>";
echo "</li>";}
?>
Your code looks alright from what I can see. Does it not work?
One way would be to get the ID through your SQL-query as well and then add the id to the href in your link. Then you can fetch it through the querystring on the other page, to display the proper post depending on which li-element the user clicked on:
echo "<a href=\"details.php?id=". $row["id"] ."\" data-transition=\"flow\">";
Not sure how you mean "somehow keep that stored within the list but not echo" - it wouldn't be possible to store anything in the list, if it is not echoed, as it then wouldn't be sent to the client. You can of course store the id in a data-attribute on the li-element, which won't be displayed to the user. It will however be visible through the source code! Don't know why that should be a problem though?