I'm trying to get all the records in my database and it was successful, however I can only get 1 id value. I need to get all the id in my database and call a javascript function. In my code I can only get the id of the last record. How can I get them all?
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");
$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0;
$message= $row['desc'];
$post = substr($message,$position,300);
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";
/*The start of chaos */
echo "<form action='' method='post' onSubmit='ReadMore()'>";
for($ctr=1;$ctr<=3;$ctr++){
$id=$ctr; //This should change and increment the value of id, isn't it?
echo "<input type='hidden' name='more' value=".$id." id='more'/>";
}
echo "<input type='submit' name='read' value='Read More'>
</form>
</td></tr>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
What's happening here is that it will display records from the database: $row['header'], $row['date'], $row['desc'] then a button which says Read More in a loop. What I would like to happen is to call a function after clicking 'read more', depending on the id.
For example,
___________________________
| This is the header |
| Date |
| Description |
| Read More Button | //this button should display $id = 3
___________________________
| This is the header |
| Date |
| Description |
| Read More Button | //this button should display $id = 2
___________________________
| This is the header |
| Date |
| Description |
| Read More Button | //this button should display $id = 1
___________________________
Is that possible? or am I missing something. My code only displays $id=1 for all the buttons. I'm sorry for this.
you may try like this
make the id field name as array like more[] and bring the form out side loop as shown
no need of for loop there to get the ids u already using while loop
bring submit button also outside loop
if u want to show the details of that post then why again u using form u can simply link it to the details page n can pass the id no need to submit it as a form .
check the code below
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");
$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";
$result = mysql_query($query);
//echo "<form action='' method='post' onSubmit='ReadMore()'>";
echo "<table>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0;
$message= $row['desc'];
$post = substr($message,$position,300);
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";
/*The start of chaos */
//for($ctr=1;$ctr<=3;$ctr++){
//$id=$ctr; //This should change and increment the value of id, isn't it?
//echo "<input type='hidden' name='more[]' value=".$id." id='more[]'/>";
//}
echo "<tr><td><a href='post_details.php?post_id=".$id."'>Read More</a></td></tr>";
</td></tr>";
echo "</tr>";
}
echo "</table>";
//echo "</form>";
mysql_close($con);
?>
Related
I'm new to mysql and php. I'm working on songs database. I have a search script which searches all columns of my database and it shows results in a table. But I want to make that search script to search by selected column field. Below the search box I want to add bullet options (ex: search by column 1, column 2, column 3, column 4) You can see a pic below.
Songs database table.
+----+--------+----------+-----+-------+
| id | title | artist | key | genre |
+----+--------+----------+-----+-------+
| 1 | xxxxx | xxx xxxxx| xxx | xxxxx |
| 2 | xxxxx | xxx xxxxx| xxx | xxxxx |
| 3 | xxxxx | xxx xxxxx| xxx | xxxxx |
| 4 | xxxxx | xxx xxxxx| xxx | xxxxx |
| 5 | xxxxx | xxx xxxxx| xxx | xxxxx |
+----+--------+----------+-----+-------+
Search script :
<?php
$host = "localhost";
$user = "xxxxxxx";
$password = "xxxxxxx";
$database_name = "xxxxxx";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$pdo->exec("set names utf8");
$search=$_POST['search'];
$query = $pdo->prepare("select * from table where title LIKE '%$search%' OR key LIKE '%$search%' OR genre LIKE '%$search%' OR artist LIKE '%$search%' LIMIT 0 , 100");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
if (!$query->rowCount() == 0) {
$row_cnt = $result->num_rows;
printf("<p class='notice'>Your search for <span class='blue'>“</span><span class='blue'>" . $result['search'] . "</span><span class='blue'>”</span> yielded %d results.\n</p>", $row_cnt);
echo "<table class='table'>";
echo "<tr class='tablehead'>";
echo "<th>Title</th>";
echo "<th>Artist</th>";
echo "<th>Key</th>";
echo "<th>Genre</th>";
echo "</tr>";
while ($results = $query->fetch()) {
echo "<tr>";
echo "<td> " " " . $results['title'] . " </td>";
echo "<td>" . $results['artist'] . "</td>";
echo "<td>" . $results['key'] . "</td>";
echo "<td>" . $results['genre'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo 'Nothing found';
}
?>
Im newbie. Please help.
UPDATE
$search=$_POST['search'];
$field=$_POST['radio_grp'];
switch($field)
{
case 'Title' : $field='title';
break;
case 'Artist' : $field='artist';
break;
case 'Category' : $field='category';
break;
}
$query = $pdo->prepare("select * from lyrics_a where $field LIKE '%$search%' LIMIT 0 , 100");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
echo"<form action='search.php' method='post'>
Search: <input type='text' id='search' name='search' placeholder='Search' required data-validation-required-message='Please enter atleast 3 characters'/>
<input type='submit' class='searchButton greenButton' value='Go' />
<input type='radio' name='search' value='title'/>Title
<input type='radio' name='search' value='artist'/>Artist
<input type='radio' name='search' value='category'/>Category
</form><br>";
// Display search result
if (!$query->rowCount() == 0) {
$row_cnt = $result->num_rows;
echo "<table class='table'>";
echo "<tr class='tablehead'>";
echo "<th>Title</th>";
echo "<th>Artist</th>";
echo "<th>Category</th>";
echo "</tr>";
while ($results = $query->fetch()) {
echo "<tr>";
echo "<td> " . $results['tel_title'] . " " . $results['title'] . " </td>";
echo "<td>" . $results['artist'] . "</td>";
echo "<td>" . $results['category'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo 'Nothing found';
}
?>
You should get values of your radio buttons and make appropriate query
$search=$_POST['search'];
$field=$_POST['radio_grp'];
switch($field)
{
case 'Title' : $filed='title' // or your column field
break;
//---similar cases
}
$query = $pdo->prepare("select * from table where $field LIKE '%$search%' LIMIT 0 , 100");
Try this code
Set the value of radio button same as column name
<input type="radio" name="search_by" value="title">title<br>
<input type="radio" name="search_by" value="artist">artist<br>
<input type="radio" name="search_by" value="key">key<br>
<input type="radio" name="search_by" value="genre">genre<br>
$search=$_POST['search'];
$search_by=$_POST['search_by'];
$query = $pdo->prepare("select * from table where $search_by LIKE '%$search%' LIMIT 0 , 100");
How to create a table with data retrieved from MySQL with PHP
-------------------------
| albumID | trackID |
-------------------------
| | 1990 |
- 1 -------------
| | 1991 |
-------------------------
| | 1992 |
- -------------
| 2 | 1993 |
- -------------
| | 1994 |
-------------------------
I can generate a table from the database, but the result will be only like each result in one row
$query = "SELECT albumID, trackID
FROM songs";
$result = mysqli_query($db, $query);
echo "<table>";
echo "<tr>\n";
echo "<th>albumID</th>\n";
echo "<th>trackID</th>\n";
echo "</tr>\n";
while ($data = mysqli_fetch_array($result)) {
echo "<tr>\n";
echo "<td>$data[0]</td>\n";
echo "<td>$data[1]</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
Any help given is really appreciated!
You can do this by using a variable to keep track of the current album and only output the album once as shown below.
$current_album = ''; # initialize tracking var
while ($data = mysqli_fetch_array($result)) {
echo "<tr>\n";
if ($current_album != $data[0]) {
echo "<td>$data[0]</td>\n";
$current_album = $data[0];
} else {
echo "<td> </td>\n"; # insert empty cell
}
echo "<td>$data[1]</td>\n";
echo "</tr>\n";
}
This will have the album ID appearing in the first row for the album. If you want it to be vertically centered you can use rowspan on the td element, but in order to know how many rows each album has you will either need to update your sql query to return that or you can process the results into a multidimensional array and then loop through that to generate the output.
Maybe not the most elegant solution, but should do the trick.
$query = "select albumID as album, group_concat(trackID) as tracks from songs group by albumID";
$result = mysqli_query($db, $query);
echo "<table>";
echo "<tr>\n";
echo "<th>albumID</th>\n";
echo "<th>trackID</th>\n";
echo "</tr>\n";
while ($data = mysqli_fetch_array($result)) {
$tracks = explode(",", $data[1]);
echo "<tr>\n";
echo "<td rowspan=\"".count($tracks)."\">$data[0]</td>\n";
foreach ($tracks as $key => $value) {
echo "<td>$value</td>\n"
}
echo "</tr>\n";
}
echo "</table>\n";
Edit your query to return the numbers of tracks of the album and use that number to create a rowspan only on the first track row. I wrote a little example but I havn't tried if it works.
$query = "SELECT s.albumID, s.trackID,
( SELECT COUNT(*)
FROM songs s2
WHERE s2.albumID = s.albumID )
FROM songs s";
$result = mysqli_query($db, $query);
echo "<table>";
echo "<tr>\n";
echo "<th>albumID</th>\n";
echo "<th>trackID</th>\n";
echo "</tr>\n";
$tmpAlbum = '';
while ($data = mysqli_fetch_array($result)) {
echo "<tr>\n";
echo "<td";
if($data[2] == 1){
echo "<td>$data[0]</td>\n";
}else{
if($data[0] != $tmpAlbum){
echo "<td rowspan=\"".$data[2]."\">$data[0]</td>\n";
}
}
$tmpAlbum = $data[0];
echo "<td>$data[1]</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
I an trying to display records in table with top 10 enteries with specific color
My Query just for Ref.
$sqlsum=mysql_query("SELECT `userid`, SUM(`points`) as `total` FROM
`tablename` GROUP BY `userid` ORDER BY total DESC LIMIT 10");
This code below displays a simple table, I need to display table With TOP 10 entries be different in colour[blue].. rest remains the same [white background].
i.e Top 10 can be in blue color, and rest in white color.
Below is the code I am using to display records.
<?php
while($row = mysql_fetch_array($sqlsum))
{
echo "<tr> ";
echo "<td>" .$row[userid] . "</td>";
echo "<td>" .$row[total] . "</td>";
}
echo "</tr> " ;
?>
I have this table structure as sample. That I want to use, with this code. The table need to be the same , but i am not to find the logic , how to build the table
with this structure
<table>
<thead><tr><td colspan="2"><center>Prizes</center></td></tr><tr>
<th>Position</center></th><th><center>Prize</center></th></tr></thead>
<tbody><tr><td>1st</td><td>0.0$</td></tr>
<tr class="alt"><td>2nd</td><td>0.0$</td></tr>
<tr><td>3rd</td><td>0.0$</td></tr>
<tr class="alt"><td>4th</td><td>0.0$</td></tr>
<tr><td>5th</td><td>0.0$</td></tr>
</tbody>
</table>
Remove Limit 10 For Fetch All Data:
$sqlsum=mysql_query("SELECT `userid`, SUM(`points`) as `total` FROM
`tablename` GROUP BY `userid` ORDER BY total DESC");
php:
<?php
$i=1;
while($row = mysql_fetch_array($sqlsum))
{
echo "<tr ".(($i <= 10) ? "bgcolor='blue'" : '')'."> ";
// Apply attrinute bgcolor for backgroung color
echo "<td>" .$row[userid] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr>";
$i++;
}
?>
Set an indicator variable to point it.
<?php
$rowNumber = 0;
while($row = mysql_fetch_array($sqlsum))
{
if ($rowNumber < 10)
{
echo "<tr class=\"alt\"> ";
}
else
{
echo "<tr> ";
}
echo "<td>" .$row[userid] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
$rowNumber++;
}
?>
I am printing a 3 column MySQL table on a webpage. Everything prints out except for the second column, its just blank.
Here is the code I'm using:
$connection = mysql_connect($hostname, $username, $password);
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("MEASURE", $connection);
$numberz = 54;
mysql_query("INSERT INTO measurement (DATA)
VALUES
('$numberz')");
$result = mysql_query("SELECT * FROM measurement");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>DATA</th>
<th>TIME</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['DATE'] . "</td>";
echo "<td>" . $row['TIME'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($connection);
This is what I get on my webpage:
ID DATA TIME
1 2013-02-26 14:32:26
2 2013-02-26 14:32:26
3 2013-02-26 14:32:27
Notice how the second column is blank
Here's how the table looks in MySQL:
ID | data | TIME
| 1 | 1 | 2013-02-26 14:32:26 |
| 2 | 1 | 2013-02-26 14:32:26 |
| 3 | 1 | 2013-02-26 14:32:27 |
Where you are printing out to the page you are accessing $row['DATE'] which looks like it should be $row['DATA']
try this:
if you table field name are ID, data, TIME. you are using capital and wrong field name.
$result = mysql_query("SELECT * FROM measurement");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>DATA</th>
<th>TIME</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['data'] . "</td>";
echo "<td>" . $row['TIME'] . "</td>";
echo "</tr>";
}
echo "</table>";
Hope this will work.
I have a Physician Query:
// Primary Physician Query
$qPhysician = mysql_query("SELECT * FROM physicians ORDER BY lastName ASC, firstName ASC");
$rowPhysician = mysql_fetch_array($qPhysician);
// State Query for Physician
$idStatePhysician = $rowPhysician['idstate'];
$qStatePhysician = mysql_query("SELECT * FROM states WHERE idstate=$idStatePhysician");
$rowStatePhysician = mysql_fetch_array($qStatePhysician);
// City Query for Physician
$idCityPhysician = $rowPhysician['idcity'];
$qCityPhysician = mysql_query("SELECT * FROM cities WHERE idcities=$idCityPhysician");
$rowCityPhysician = mysql_fetch_array($qCityPhysician);
I have a while loop to display all physicians row to a table:
$num = mysql_num_rows($qPhysician);
$i=0;
while($i < $num)
{
$idphysicians = $rowPhysician['idphysicians'];
if ($i % 2 == 0){
echo "<tr class='even' onclick=\"DoNav('physicianUpdate.php?idphysicians=$idphysicians');\">";
}
else{
echo "<tr class='odd' onclick=\"DoNav('physicianUpdate.php?idphysicians=$idphysicians');\">";
}
echo "<td>" . mysql_result($qPhysician,$i,"lastName") . "</td>";
echo "<td>" . mysql_result($qPhysician,$i,"firstName") . "</td>";
echo "<td>";
if(isset($rowPhysician['idcity'])){echo mysql_result($qCityPhysician,$i,"name");} else{}
echo "</td>";
$i++;
}
My problem is: I have 3 rows of data from my physicians table. Each has a value for 'idcity' reflecting the idnumber from my City table. However, the 1st row of Data displays the idcity=Name properly, but the 2nd and 3rd row gave an error:
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 7 in C:\wamp\www\iPOC\physicians.php on line 55
Also, if I have a blank value for idcity on one of the row, it also generates an error.
Please help! Thanks in advance!
The problem is that you're using mysql_result() with a one-way result. The correct fix is to use one of the mysql_fetch_*() functions instead, checking the returned value in your while loop.
while($row = mysql_fetch_array($qPhysician)) {
...
}
Something like this would probably work better:
$qCityPhysician = mysql_query("SELECT * FROM cities WHERE idcities=$idCityPhysician");
$qCityPhysicians = array();
while($row = mysql_fetch_array($qCityPhysician)) {
$qCityPhysicians[$row['idcity']] = $row['name'];
}
$qPhysician = mysql_query("SELECT * FROM physicians ORDER BY lastName ASC, firstName ASC");
$i=0;
while($row = mysql_fetch_array($qPhysician)) {
if ($i % 2 == 0) {
echo "<tr>";
echo "<td>" . $row['lastName'] . "</td>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>";
if(isset($row['idcity'])) {
echo $qCityPhysicians[$row['idcity']];
}
echo "</td>";
$i++;
}
}