i'm creating my first PHP/MySQL site and i'm having difficulty figuring out how to generate dynamic links and creating a new page for those links.
My index page is pulling in certain details from my database as a preview, and when the visitor clicks on that item, i want them to be taken to a page which shows the full information from the database for that row.
The code on my index page for displaying the previews is below, any help on amending it to generate the link and page would be greatly appreciated.
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle"><?php echo $f2; ?></li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
</div>
<?php
$i++;
}
?>
I'm pretty sure what i'm asking is really simple, i just can't get my head around acheieving it.
Thanks to you both for your answers, but i have managed to fix it (or work-around it) with this on my index page:
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
do {?>
<div class = "hjl"><a href="paging.php?job_id=<?php echo $rsjobinfo['job_id'];?>">
<ul>
<li id = "jobtitle"><?php echo $rsjobinfo['job_title'];?></li><br />
<li id = "compname"><?php echo $rsjobinfo['company_name'];?></li>
</ul>
<ul>
<li id = "city"><?php echo $rsjobinfo['city'];?>,
<?php echo $rsjobinfo['country'];?></li>
</ul>
</a>
</div>
<?php } while ($rsjobinfo=mysql_fetch_assoc($result))?>
</div>
Followed by this on my content page:
<?php
$job_id = $_GET['job_id'];
$query="SELECT * FROM $tbl_name WHERE job_id = $job_id";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
?>
Thanks for your help everyone.
Dan
put mysql_close after you use mysql_result, but once you get it working you might look into a more modern approach like PDO.
to your code add link (which I think you already have somewhere):
//...................
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li>
//...................
Read more...
//...................
then your code must check for variable $_GET['id'], so put IF in the beginning of your code:
$where = '';
if( isset($_GET['id']) && strlen($_GET['id']) > 0 ) {
$where = ' job_id = "'. mysql_real_escape_string( $_GET['id'] ) .'"' ;
}
<?php
$query="SELECT * FROM $tbl_name $where ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
Read more...
</div>
<?php
$i++;
}
?>
edit: Try changing the following line:
$where = " job_id = '". mysql_real_escape_string( $_GET['id'] ) ."'" ;
Related
<?php include ("../navbar.php"); ?>
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn,$query);
?>
<div class="container">
<div class="compound_report_card">
<h2>Compound Report</h2>
<div class="card-content">
<?php
//Check If Id Isset.
if(isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET[ "CHIHBT_ID" ] . "</p>";
$result = mysqli_query($conn, $query);
?>
<?php
while($row = mysqli_fetch_assoc($result )) {
?>
<ul>
<li>Canonical smiles : <?php echo $row['smile_notation']; ?> </li>
<li> Molecular weight : <?php echo $row['molecular_weight']; ?></li>
<li> Heavy atoms : <?php echo $row['number_of_heavy_atoms']; ?></li>
<li> Aeromatic heavy atoms : <?php echo $row['number_of_aeromatic_heavy_atoms']; ?></li>
<li> Rotatable bonds : <?php echo $row['number_of_rotatable_bonds']; ?></li>
<li> H bond acceptors : <?php echo $row['number_of_h_bond_acceptors']; ?></li>
<li> H bond donors: <?php echo $row['number_of_h_bond_donors']; ?></li>
<li> Tpsa A**2 : <?php echo $row['tpsa']; ?></li>
</ul>
<?php } ?>
</div>
</div>
</div>
<?php include ("../footer.php"); ?>
so this is my new code and i am displaying data on web from my database and . So what i am trying to do is i have a lot of id's and i want that when i click on any id the data of that id from
database should get displayed. So the current situation is that when i am clicking on one id whole data from that table is getting displayed and i want only one to get displayed. I want onlyspecific data from every id that i click. So the problem i think is in while loop so please see that also
The problem: Your code is getting all records and isn't querying against your selected ID.
What you need to do: Add selected ID in query to get specific result. Like shown below:
Update: From What I understand $_GET["CHIHBT_ID"] is your ID, right? Change this code in your script:
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn, $query);
?>
To
<?php include("../conn.php");
$query = "select * from semi_synthetic";
?>
Then, change the following code:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
$result = mysqli_query($conn, $query);
?>
With this:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"])) {
$query .= ' WHERE CHIHBT_ID = ' . $_GET["CHIHBT_ID"]; // add selected id in query
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
}
$result = mysqli_query($conn, $query); // run query
?>
and that's it.
Here is the challenge and I am sure this is not the first time someone has came into this issue however have not found a viable solution pertaining to this specific process within the pages of stackoverflow or any other development forum for that matter.
Here is the task I am trying to complete.
I have built a WordPress website which will be a members area where only registered users may log into the site to view its content. This functionality will be done through WordPress however I have a page within WordPress titled "Member Status" that I have created a custom page template for in order to pull information that will be user specific to a form on that page. The user specific information is being pulled from a secondary external database to the WP database.
Here is the code for the custom page template for a better visual of what I am trying to do. Basically all form fields would be populated from the specific row of that specified user. however the query seems to fail everytime and am wondering if I should be adding this table to my WP db, if that would rectify the issue or is there a fault in my code I am just not seeing.
<?php
$servername = "localhost";
$username = "DBusername";
$password = "DBpassword";
$database = "DBname";
// Create connection
$conn = new mysqli($servername, $username, $password);
$db_selected = mysqli_select_db($conn, $database);
$userid = $current_user->user_login;
$sql = "SELECT * FROM 'member-status' WHERE 'Card' = '$userid'";
$result = mysqli_query($conn, $sql);
if ($result) {
list($userid) = mysqli_fetch_array(mysqli_query($result));
} else {
echo "query failed";
}
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
while ($row = mysqli_fetch_assoc("$sql"))
?>
<div class="Disclaimer_webpage">
<?php echo $row["Disclaimer_WebPage"]; ?>
</div>
<div class="OuterBlock">
<div class="oneblock">
<ul>
<li>LAST</li>
<li><?php echo $row["Last"]; ?></li>
</ul>
<ul>
<li>FIRST</li>
<li><?php echo $row["First"]; ?></li>
</ul>
<ul>
<li>CARD</li>
<li><?php echo $row["Card"]; ?></li>
</ul>
<ul>
<li>CLASS</li>
<li><?php echo $row["Class"]; ?></li>
</ul>
</div>
<div class="secblock">
<ul>
<li>DATE SIGNED BOOK</li>
<li>
<?php if ($row['StartDate']!=""){ echo date('m/d/y', strtotime($row["StartDate"])); } ?>
</li>
</ul>
<ul>
<li>OUT OF WORK DATE</li>
<li>
<?php if($row['CURROOWD']!=""){ echo date('m/d/y', strtotime($row["CURROOWD"])); } ?>
</li>
</ul>
<ul>
<li>DISPATCH UNIT</li>
<li><?php echo $row["OriginalUnit"]; ?></li>
</ul>
<ul>
<li>DAYS DISPATCHED (OF 140)</li>
<li><?php echo $row["DAYS140"]; ?></li>
</ul>
</div>
<div class="over_all_list_position">
<ul>
<li><b>OVER ALL LIST POSITION</b><span class="information_Icon"><img src="http://redsealcreative.com/clients/353-members/wp-content/plugins/IBEWJOBLISTS/Info.png" > <p class="imgDescription">Your position on the Out of Work List.</p></span><span class="see_below"> (*see below)</span></li>
<li><?php echo $row["ListPosition"]; ?></li>
</ul>
<ul>
<li><b>UNIT LIST POSITION</b><span class="information_Icon1"><img src="http://redsealcreative.com/clients/353-members/wp-content/plugins/IBEWJOBLISTS/Info.png" > <p class="imgDescription1">Your list position relative only to members in your Dispatch Unit.</p><span></li>
<li><?php echo $row["LISTPOS_UNIT"]; ?></li>
</ul>
<ul>
<li><b>OPG LIST POSITION</b><span class="information_Icon2"><img src="http://redsealcreative.com/clients/353-members/wp-content/plugins/IBEWJOBLISTS/Info.png" > <p class="imgDescription2">Your list position relative only to those with current OPG Security Clearance.</p><span></li>
<li><?php echo $row["LISTPOS_OPG"]; ?></li>
</ul>
</div>
<div class="fothblock">
<ul>
<li>PASSES</li>
<li>NORTH UNIT</li>
<li>SOUTH UNIT</li>
<li>EAST UNIT</li>
</ul>
<ul>
<li>ACCUMULATED</li>
<li><?php echo $row["PassNorthAcc"]; ?></li>
<li><?php echo $row["PassSouthAcc"]; ?></li>
<li><?php echo $row["PassEastAcc"]; ?></li>
</ul>
<ul>
<li>MAXIMUM</li>
<li><?php echo $row["PassNorthMax"]; ?></li>
<li><?php echo $row["PassSouthMax"]; ?></li>
<li><?php echo $row["PassEastMax"]; ?></li>
</ul>
</div>
<div class="sixblock">
<ul>
<li>LAST OUT WORK DATES DISPATCHED</li>
<li>NORTH</li>
<li>SOUTH</li>
<li>EAST</li>
<li>OUT OF TOWN</li>
</ul>
<ul>
<li class="padding-top"></li>
<li class="padding-top"><?php if($row['LOOWDN']!=""){echo date('m/d/y', strtotime($row["LOOWDN"])); }?></li>
<li><?php if($row['LOOWDS']!=""){ echo date('m/d/y', strtotime($row["LOOWDS"])); } ?></li>
<li class="padding-top"><?php if($row['LOOWDE']!=""){echo date('m/d/y', strtotime($row["LOOWDE"]));} ?></li>
<li><?php if($row['OOTOOWD']!=""){echo date('m/d/y', strtotime($row["OOTOOWD"]));} ?></li>
</ul>
</div>
<div class="seven">
<ul>
<li>LAST UPDATED</li>
</ul>
<ul>
<li class="padding-top"><?php echo $row["TIME_STAMP"]; ?></li>
</ul>
</div>
</div>
<div class="Disclaimer_POL">
<?php echo $row["Disclaimer_POL"]; ?>
</div>
All insight will be gratefully appreciated. Thanks in advance for the communities assistance to a new member. :)
UPDATE #2 ISSUE RESOLVED
I am happy to announce that I figured out the issue. For anyone else that is wanting to accomplish this task here is the working code that I used.
<?php global $current_user;
wp_get_current_user(); ?>
<?php
// $mydb = new wpdb('username','password','database','localhost');
//$mydb->show_errors();
$userid = $current_user->user_login;
$result = $wpdb->get_results( "SELECT * FROM member_status WHERE CARD = $userid");
// $query = "SELECT * FROM member_status WHERE CARD = $userid";
// $result = $mydb->get_results($query);
?>
<?php foreach ( $result as $query ) {?>
Then to call the individual fields/column data add this code where you want it located
<?php echo $query->ColumnName; ?> //Change ColumnName to the column you want to call from.
I hope this comes in handy for you all :)
I am happy to announce that I figured out the issue. For anyone else that is wanting to accomplish this task here is the working code that I used.
<?php global $current_user;
wp_get_current_user(); ?>
<?php
// $mydb = new wpdb('username','password','database','localhost');
//$mydb->show_errors();
$userid = $current_user->user_login;
$result = $wpdb->get_results( "SELECT * FROM custom_table WHERE Column = $userid");
// $query = "SELECT * FROM custom_table WHERE Column = $userid";
// $result = $mydb->get_results($query);
?>
<?php foreach ( $result as $query ) {?>
Then to call the individual fields/column data add this code where you want it located
<?php echo $query->ColumnName; ?> //Change ColumnName to the column you want to call from.
I hope this comes in handy for you all :)
i have created navigation using mysql php. now i am trying to pass value to another page using ajax.please help me
<ul id="all_navs">
<?php $sql = mysql_query("select * from sub_category_master where main_category =1");
while($row = mysql_fetch_array($sql))
{?>
<li id="s1">
<a href="#tab_deals" onclick="showUsers('<?php echo $row['sub_category_id']; ?>')" id="insert">
<?php echo $row['sub_category_name']; ?></a>
</li>
<?php }?>
</ul>
if i understood your problem why not try something like this:
<ul id="all_navs">
<?php $sql = mysql_query("select * from sub_category_master where main_category =1");
while($row = mysql_fetch_array($sql)){
echo '<li id="s1">';
echo '<a href="#tab_deals" onclick="showUsers('.$row['sub_category_id'].')" id="insert">';
echo $row['sub_category_name'].'</a>';
echo '</li>';
}
?>
</ul>
it would help more if you told us what the problem was.
I have a problem with the following code:
$query = "SELECT * FROM movie_list WHERE id=$id";
$result_q = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result_q);
if ($row['movie_type'] == 'E' || $row['movie_type'] == 'S'){
$query_serie = "SELECT * FROM movie_list WHERE id_serie=$row[id_serie]";
$result_q_serie = mysqli_query($conn, $query_serie);
$query_serie_cont = "SELECT serie_number FROM movie_list WHERE id_serie = $row[id_serie] AND serie_number IS NOT null GROUP BY serie_number";
$result_q_serie_cont = mysqli_query($conn, $query_serie_cont);
[...]
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
}
?>
</li>
It works perfect the first while but the second time the while($row_serie) variable are missing. Debugging the page I see the $row_serie variable disappear after completed all the first while($row_serie_cont) but not reappearing at all when it was triggered the second time.
What I've miss in the code?
you close li after loop make it correct
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
echo "</li>"// inside the loop
}
?>
Seems that insert $result_q_serie = mysqli_query($conn, $query_serie);into the second while do the trick.
I don't understand why i need to repopulate result_q_serie is needed but now it works.
If someone can propose a better solution I'm hearing
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li class="no-padding">
<ul class="collapsible collapsible-accordion">
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?>
</a>
<div class="collapsible-body">
<ul>
<?php
$result_q_serie = mysqli_query($conn, $query_serie);
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>
".$row_serie['episode_number']."</li>
"; } } echo "
</ul>
"; echo "
</div>
</li>
</ul>
</li>
"; } ?>
</ul>
seems that use mysqli_data_seek($result, 0); do the trick the right way.
I'm new to coding with php and using MySQL. I am having trouble to display a list of categories by their ID so that each category is displayed individually as a heading. Instead I got it to display a category name but its only echoing out a category name twice that's the same. Here is my code...
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
$id=$row['id'];
$cat_name=$row['cat_name'];
}
?>
<ul class="nav nav-list">
<li class="nav-header"><?php echo $cat_name;?></li>
<li class="nav-header"><?php echo $cat_name;?></li>
</ul>
Your li tag is outside the while loop. So the $id and $cat_name is only the last record in the DB, then you echo them twice. That's way you got the same name twice.
Try echo the li tag in the loop (but not the ul):
<ul class="nav nav-list">
<?php
while($row = mysql_fetch_array($query))
{
$id=$row['id'];
$cat_name=$row['cat_name'];
echo '<li class="nav-header">' .$cat_name. '</li>';
}
?>
</ul>
You can use this code as-is :
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql); ?>
<ul class="nav nav-list">
<?php while($row = mysql_fetch_array($query)) : ?>
<li class="nav-header"><?php echo $row['cat_name'];?></li>
<?php endwhile; ?>
</ul>
This will loop through your records and for each record, it will print an entire li with the required data.
Note that separating your PHP code from your HTML code like this has several benefits. It will be better colored in your editor and it is also easier to integrate.
The reason you are printing the same value out twice is because $cat_name is a string variable and will only hold one value at a time you may want to save the items an array and loop at a seperate time, like such
<?php
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
$category = array();
while($row = mysql_fetch_array($query))
{
$array = array(
id => $row['id'],
name => $row['cat_name']
);
array_push($category,$array);
}
?>
<ul class="nav nav-list">
<?php
foreach($category as $c)
{
echo '<li class="nav-header">'.$c['name'].'</li>';
};
?>
</ul>
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{
?>
<ul class="nav nav-list">
<li class="nav-header"><?php echo $row['id']; ?></li>
<li class="nav-header"><?php echo $row['cat_name']; ?></li>
</ul>
<?php } ?>