I'm coming here after some head banging. Still learning php/mysql and coming across an issue which is bothering me for a while.
Currently I print a table with all the data without any condition. The values below the lower values and above are printed in this table. The snippet of my code is below:
while ($result = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" .$result['id']. "</td>";
if ($result['time'] > $limit[lower]){
echo "<td bgcolor=#ff0000>" $result['time']. "</td>";
} else {
echo "<td>" .$result['time']. "</td>";
}
echo "<td>" .$result['name']. "</td>";
echo "</tr>";
}
I want to create a href link so that by dafault only values above the lower are printed and on clicking the link, say detailed_view will give the table as it does above i.e. all value including below and above the lower value. Shall I create two methods and use them in href separately. In that case there will be two while loops which I want to avoid. Any pointer on this?
The name of the script is display.php, so before the loop I tried using:
echo "<a href='display_jobid.php?view=$num'>Show All</a>";
It does create a hyperlink Show All but how to get the functionality working, am not sure.
Thanks
Related
A PHP page displays only one word from a MySQL record in a URL variable, while it displays the correct 2 words in the HTML output, I tried many solutions such '".$row['book_category']."' and {$row['book_category']} etc. but I still get the same one result.
<?php
foreach($conn->query('SELECT book_category, COUNT(*) FROM books GROUP BY book_category') as $row) {
echo "<tr>";
echo "<td>" . "" . $row['book_category'] . "" . "</td>";
echo "</tr>";
}
?>
So now first $row insert is only one word in the url, while the second $row outputs two words properly as expected; the problem is I need the two words to be passed as variable in the URL.
URLs cannot have a space character, this is why you are seeing the first word only, you need to encode the value received using rawurlencode().
<?php
foreach($conn->query('SELECT book_category, COUNT(*) FROM books GROUP BY book_category') as $row) {
echo "<tr>";
echo "<td>" . "<a href='sidebar_cat_display.php?book_cat=" . rawurlencode($row['book_category']) . "'>" . $row['book_category'] . "</a>" . "</td>";
echo "</tr>";
}
?>
Keep in mind you will also need to adjust the sidebar_cat_display.php page to fetch the book_cat parameter from the URL accordingly if you aren't already.
I am have a page in which i have to show all the records from my data base but its taking alot of time when i refresh it.
The html part loads fast but where the cursor reaches my mysql query it takes a lot of time to load..
here is my query
$srchresult = mysql_query("SELECT `date`,`chalno`,`custcode`,`vessel`,`rankcode`,`crew`,`ppno`,`cdcno` FROM `$maindb` WHERE `series`='$zseries' Order By `date`, `chalno` desc")or die(mysql_error());
while ($row = mysql_fetch_array($srchresult)) {
$mdate = strtotime("d-m-Y", $row[date]);
echo "<tr>";
echo "<td align='left'>$mdate</td>";
echo "<td align='left'>$row[chalno]</td>";
echo "<td align='left'>$row[custcode]</td>";
echo "<td align='left'>$row[vessel]</td>";
echo "<td align='left'>$row[rankcode]</td>";
echo "<td align='left'>$row[crew]</td>";
echo "<td align='left'>$row[ppno]</td>";
echo "<td align='left'>$row[cdcno]</td>";
echo "<td align='left'><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></td>";
echo "</tr>";
}
}
I strongly recommend using pagination in order to be able to render/browse trough 10k records on a page.
If you must do it without a visible pagination block in your layout then use an infinite scroll solution. Here is a rather simple one: http://www.infotuts.com/ajax-infinite-scroll-using-jquery-php-mysql/
Your mysql query is fine, even though you should use at least PHP's mysqli extension (http://php.net/manual/en/intro.mysqli.php) to retrieve the data.
So currently, I'm making a music database online. The part I'm trying to implement is a playlist function so that given a list of your playlists, you can click one and view the music. What I have is this:
if($result = mysqli_query($link, $playlist))
{
if(mysqli_num_rows($result) > 0)
{
echo "<h1 style='text-align:center;'>Playlists</h1>";
echo "<table style='width:100%'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . '<a href="getPlaylist.php>' . $row['name']."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
So I need it so that once someone clicks the link, it will transition them into getPlayList.php and also send over the name in the link, (technically $row['name']) so that I can actually pull up the information in the SQL database. I've tried doing things like:
echo "<td>" . '<a href="getPlaylist.php?link=' . $row["name"]. '">' . $row['name']."</td>";
but to no avail. Can anyone help?
edit:
So what this segment is supposed to do is, it makes a table with each row having a link to a different playlist, but they all go to "getPlaylist.php" where using the name of the link clicked, it will then retrieve all the songs that exist inside it.
edit: solved thanks to RamRaider
I had to use $_GET['link'] in getPlaylist.php
I am opening a file (formatted in rows and columns) with PHP, looping through the rows on the file and echoing out rows into a table that meet a certain criteria. On each row that I echo out I am wrapping it in form tags. Ultimately, if I echo out 20 rows I will echo out 20 forms, each of them having a button. I am also echoing out a column with a comments box. I want the end user to be able to go to this page, enter comments on each row, and then hit the submit button to store the results of the row into a database. I have 2 problems that I do not know how to overcome.
1.) When echoing out the results to the table I do not create a static variable to reference when I go to create my SQL insert statement. How do I label each piece of data so I can call it later?
2.) There are going to be multiple forms, and the user will only be using one at a time. How do I ensure that when the user clicks submit, it only submits the data fields from the same row?
Hopefully this makes sense. If not Im happy to add as much clarification as needed. Im definitely a newbie when it comes to PHP, so I'm certain this is a poor design but Im on a tight timeline and I just need a working product for now. Next week I can go back and perhaps implement a better solution I'm sure one you kind people will suggest:)
<?php
if (($handle = fopen("name of file to open here", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if ($data[4] == $login){
$num = count($data);
echo "<form action='analyzer.php' method='post'>";
echo "<tr>";
echo "<td>";
echo $data[1];
echo "</td>";
echo "<td>";
echo $data[2];
echo "</td>";
echo "<td>";
echo $data[3];
echo "</td>";
echo "<td>";
echo $data[4];
echo "</td>";
echo "<td>";
echo $data[5];
echo "</td>";
echo "<td>";
echo $data[6];
echo "</td>";
echo "<td>";
echo $data[7];
echo "</td>";
echo "<td>";
echo $data[8];
echo "</td>";
echo "<td>";
echo "<input type='text' name='comments' />";
echo "</td>";
echo "<td>";
echo "<input class='mybutton' type='submit' name='#' value='Submit' />";
echo "</td>";
echo "</tr>";
echo "</form>";
}
fclose($handle);
}
}
?>
1.) When echoing out the results to the table I do not create a static variable to reference when I go to create my SQL insert statement. How do I label each piece of data so I can call it later?
You need some way of uniquely identifying the piece of data. You can do this using hidden input variables.
2.) There are going to be multiple forms, and the user will only be using one at a time. How do I ensure that when the user clicks submit, it only submits the data fields from the same row?
You can name the forms or assign them a sequence in an array. For example:
<input type="submit" name="form[5]" />
You can also simplify your code by using a loop like this:
foreach( $data as $key=>$val ) {
echo "<td>$val</td>";
}
I know there is already an accepted answer here, but I feel like suggesting another way of doing this. Having a bunch of forms on one page each with their own submit button and requiring a page load every time is kind of cumbersome from a UI perspective.
My suggestion is that you start the long, painful, and rewarding process of learning Ajax. I have been learning jQuery AJAX and I use it to do similar things.
Example:
I have a table with 30 rows, each of which represent a process.
Each row has a checkbox.
I have a submit button down at the bottom, where a user can submit all the rows with checked boxes.
But, I also have buttons in each row (right next to the checkboxes actually). The buttons are simply labeled "now", and when you click them, that row is processed in the background without a page reload.
So I have allowed users to submit many rows with the normal form submit button if they want, OR to submit any individual row and not have to wait for the page to reload.
I can perhaps add some code here if anyone is ever interested.
I am trying to output a table using php variables like this:
echo "<tr>";
echo "<td>".$var1."</td>";
echo "<td>".$var2."</td>";
echo "</tr>";
It works fine if both variables exist.. but if $var1 is null, the $var2 value is outputted to the firs column in the HTML table.
Is there a way to properly output the columns by naming them?
P.S: I can't do "if $var == NULL ...." for some coding reasons. I want to control the HTML output if possible.
While I agree with #leeb above, do you want something like this:
echo "<td>" . isset($var1)?$var1:"" . "</td>";
echo "<tr>";
echo "<td>".$var1." </td>";
echo "<td>".$var2." </td>";
echo "</tr>";