I am trying to implement dashboard kind of system. And having problem with passing variable in navigation menu items.
At first user selects "Workspace Name" from list of workspaces.
That link redirects user to projects.php?workspaceid=xxxxxx
Now inside project.php , there is sidebar (static) with other options like
projectsettings.php , credentials.php.
I want to pass variable workspaceid in each sub menu items.
Like projectsettings.php?workspaceid=xxxxxx or credentials.php?workspaceid=xxxxxx
How can I do so ?
UPDATE :
I think i was quite unclear about question.
At first user comes to workspaces.php
It looks like
![workspace selection table]
http://i.stack.imgur.com/eBtSm.png
Then for example they select "internet explorer 7" from table.
Link will take them to project.php?workspaceid=IE7
It looks like
![Project dashboard]
http://i.stack.imgur.com/EfdK1.png
Here all menus (data menus, data variable, setting.php) should have variable of workspace.
You can do something like:
<?php
if(isset($_GET['workspaceid']))
$workspaceid = htmlspecialchars($_GET['workspaceid']);
else
$workspaceid = '';
$query = "SELECT * FROM table WHERE workspaceid = ?";
if($stmt = $mysqli->prepare($query)){
$stmt->bind_param('i',$workspaceid);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
$variablename = $row['columnname']; //put your own in here
}
$stmt->free_result();
$stmt->close();
}else die("Query failed to prepare");
The you can navigate by using credentials.php?workspaceid=xxxxxx"
EDITED ANSWER:
You need to set a session once the workspaceid is set
<?php
session_start();
if (isset($_SESSION['workspaceid'])){
//do what you need with this info
}
else if (isset($_GET['workspaceid'])){
//lets set the session
$_SESSION['workspaceid'] = trim(strip_tags($_GET['workspaceid']));
}
else {
//do nothing
}
//with this method there is no need to pass the workspaceid via url
?>
Original answer:
echo the variable in each link you need.
link text
With get variables you should be careful. I usually check and clean them in the header. So a more advanced way to do this is as follows
<?php
if (isset($_GET['workspaceid']){
$workspaceid = trim(strip_tags($_GET['workspaceid']));
}
?>
Then in the link check for isset as well
link text
Related
I am creating PHP products view details page this script disaply like this URL
http://localhost/zblog/source.php?srcid=1 I want to get title URL from id
like this
http://localhost/zblog/source.php?title=some-title
Here is my code
<?php
include("config.php");
if(isset($_GET['srcid'])) {
$srcid = $_GET['title'];
if($stmt = $con->prepare("SELECT * from products WHERE srcid=?")){
$stmt->bind_param("s", $srcid);
$stmt->execute();
}
$result = $con->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
}
}
}
?>
Your logic is skewed, if the URL is:
http://localhost/zblog/source.php?srcid=1
then you aren't passing the title but if the URL like this:
http://localhost/zblog/source.php?title=some-title
then your if statement will never run because:
if(isset($_GET['srcid'])) {
it relies on $_GET['srcid'].
If you want to use the title then amend that if statement to look for $_GET['title'] instead of $_GET['srcid'] or, try passing both parameters in the URL together.
This would only work if your url included ?srcid=1&title=some-title
This is because of your if statement which only runs if srcid is set.
change
if(isset($_GET['srcid'])) {
to
if(isset($_GET['title'])) {
Picture this: You have two different pages. One is the page the form is on. The other one is used as the form's landing page (with GET statement, ex. /newpost.php?recoverPost=115). What you want to do is ignore all the code that is used for the form when there is no ID set. How do I achieve that? I tried with the following PHP If statements:
$id = $_GET['recoverPost'];
if($id != NULL)
{
//yadayada
}
and
$id = $_GET['recoverPost'];
if(isset($id))
{
//yadayada
}
No success. What can I do to detect whether the ?recoverPost is set or not?
Edit: Snippet:
if (isset($_GET['recoverPost']))
{
include('../includes/db_connect.php');
$query_recover = $db->prepare("SELECT title, body, category_id, post_preview FROM deletedPosts WHERE post_id=$id");
$query_recover->execute();
$query_recover->bind_result($title, $body, $category_id, $post_preview);
}
Where did you declare $id? All query string parameters are passed to the $_GET array, so you'd need to check if $_GET['recoverPost'] is set.
if (isset($_GET['recoverPost'])) {
// your code here
}
I have code for a search function here:
<?php
include("config.php");
$search = mysql_real_escape_string($_POST['search']);
if (!empty($_POST['search'])) {
$data = mysql_query("SELECT * FROM shop WHERE name LIKE '%$search%' OR shout LIKE '%$search%' ORDER BY id DESC ") or die(mysql_error());
while ($info = mysql_fetch_array($data)) {
$name = stripslashes($info['name']);
$shout = stripslashes($info['shout']);
//Result code goes here eventually
}
}
?>
and I'll eventually make it display all results in a grid.
My problem starts when a user clicks on an item to see a page devoted to just that item (like any retail store site).
I'm assuming in my search code I can use $_SESSION and just set all the columns to sessions and then echo them on my results.php page.
Is this an efficient way to do this or am I going about it wrong?
I see websites that when you click on a certain item the URL looks all crazy and what not and I'm not really sure what causes that.
You can use
header('Location: /search/'.rawurlencode($search));
exit;
and now you can get your search string by
$search = rawurldecode($_GET['your_name']);
and your first page will not differ from the others. Or you can use method="get" in your search form. Anyway, you can use session to store your search string.
It is a self-submitting Form. I have 3 independent Select lists whose selected values are passed as the WHERE parameters for the sql query.
When I click on the button, all data are displayed just fine, well LIMITed, and the link pages are below. But:
When I get to click on the page links, the result set and all the links disappear from sight (only the select lists remain in sight). I believe it is related to the isset condition that, if the values have not been set, display the form, if the values have been set, then process the form. So, the first result set displays correctly, but when you try to go to the second page my form interprets that the Select Lists are not set because they are reset and because i have not clicked on the button if i click on the links instead and therefore, it wipes out everything that it not an html Form.
There is no error in the paginating code. The error is in how I link this form with this paginating method. And I can't get to know how I should.
if (isset($_POST['submitted'])) { /***************IF IT IS SET, WE PROCESS THE VALUES*************************/
$oldcountry = FALSE;
$oldfrom = FALSE;
$oldinto = FALSE;
// Here just checking that all variables have been selected
if (isset($_POST['country']))
{
$oldcountry = $_POST['country'];
$country = filter_var($oldcountry, FILTER_SANITIZE_STRING);
}
else {
echo 'Please, select a country';
}
if (isset($_POST['from_language']))
{
$oldfrom = $_POST['from_language'];
$from_language = filter_var($oldfrom, FILTER_SANITIZE_STRING);
}
else {
echo 'no has metido el from language';
}
if (isset($_POST['into_language']))
{
$oldinto = $_POST['into_language'];
$into_language = filter_var($oldinto, FILTER_SANITIZE_STRING);
}
// so, once we have selected them and clicked the button, we go for the query
// and paginate the results
require_once('bdd1.php');
$per_page = 6;
$pages_query = mysql_query("SELECT COUNT(FName)
FROM work_assignment, developer
WHERE AES_DECRYPT(country, 'elperrodesanroquenotienerabo') = '".$country."'
AND from_language = '".$from_language."'
AND into_language = '".$into_language."'
AND work_assignment.developer_id = developer.developer_id
ORDER BY FName ASC ");
$pages = ceil (mysql_result($pages_query, 0) /$per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT FName
FROM work_assignment, developer
WHERE AES_DECRYPT(country, 'elperrodesanroquenotienerabo') = '".$country."'
AND from_language = '".$from_language."'
AND into_language = '".$into_language."'
AND work_assignment.developer_id = developer.developer_id
ORDER BY FName ASC LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query)){
echo '<p>', $query_row['FName'], '</p>';
}
if ($pages >=1) {
for($x = 1; $x <=$pages; $x++){
echo ''.$x.' ';
}
}
} /************* END OF IF ISSET TO PROCESS *******************************/
?>
UPDATE UPDATE
After all the help, I managed to write a URL string that makes the page links work. Yet, still I need to tweak it as I hardcoded the name of the country (basically Estonia) instead of embedding the variable that represents it, but I can't make that URL properly be written:
This works:
echo '<a href="?country=Estonia&from_language=Russian&into_language=Latvian&submitted=true&
page='.$x. '">'.$x.'</a> ';
I would just need to replace Estonia by $country, and Russian by $from_language and Latvian by $into_language. But I have tried all possible combinations of single and double quotes and dots and I get syntax errors. Does anybody know how to write that?
The POST variables are lost because the form isn't submitted when you click the link, just as Slomo pointed out.
Unless you want those values hidden from the user, you may set the form's method to GET instead of POST. That way the variables are added to the URL, just like $_GET['page'].
The links would then have to be rendered in a way that keeps the URL intact except for the page variable, that needs to be changed. It can be done like this (just change currentpage to page):
A problem of a repeated parameter in the pagination links?
Update:
If you want to insert variables inside a string you can either place them inside double quotes (and escape other double quotes inside the string), or concatenate the string and variables, see these two examples:
echo "$x";
echo '' . $x . '';
$_POST values are only set if information is sent with a POST request, for example your has most likely method=POST. When you submit your form with method POST (button=submit) the $_POST values are set. Whenever you klick another link, only a request for a new page will be sent to the server.
If you need the information to be available after the first form submit, you should use a Session. (http://php.net/manual/de/features.sessions.php)
Sessions are stored for each client (webbrowser) and are available until the session times out (please see manual). You would have to set the session variables the first time the form gets submitted. Afterwards you can use the values from the session.
/Edit
If you do not want to use a session, you could add the filter criteria as url parameters (index.php?groupby=country) to the normal links. They would then be accessed with $_GET.
I already have an script where it will allow a user whom is logged in to comment on other users. One field for the usercommenting "men_id" and another field for the user being commented on commented_men_id. Well I save it in a comment table and to pull it I make a while I make a select to get the comment of men_id while getting the comment I do another while loop inside the while loop to get the user name and id to return it in the comment. Now the next step is to let other user to comment on top of the comment that is already there. I was wondering if I have to make another table or just create another table to get the comments on another comments. I was also wondering in terms of the php script will I have to create another while loop inside the second while loop to pull the subcomments?
So far I have the next structure
$sql1 = "SELECT id, mem_id, commented_id, comment
FROM comments
WHERE commented_id = '$id'";
while($row=msql_fetch_array($sql)) {
$id_coment = $row['id'];
$mem_id = $row['mem_id'];
$comment = $row['comment'];
$sql_member_data = msql_query("SELECT id, member_name FROM users WHERE id ='$id_coment'");
while($row2=msql_fetchj_array($sql_member_data)) {
$user_id =$row2['id'];
$user_name =$row2['member_name'];
echo '<div>'.$user_name.'</div>';
echo '<div>'.$comment.'</div>';
}
}
I advise might not be the best code but it is posting the comment, Now how can I get a comment within the comment generated by this script.
Thank you guys.
If you truly want a something like Facebook's commenting system, you are going to have to do a lot more than that. I made my own little system and it's nicely styled with some really awesome jQuery effects.
Here's what you are going to need
Section to get all your comments (which you have -- check for syntax errors)
Form and script to post your comments
And you will probally need to use jQuery and AJAX for the commenting and some more jQuery to auto-refresh like facebook does.
That's my take on it. No one else hate on me for this, just trying to give some input on it.
<?php
// Connect to database here
// Search and start loop to get all comments
$sql_comments = mysql_query("SELECT * FROM comments WHERE type='main'");
while($rows_comment=mysql_fetch_array($sql_comments)){
// Get comment information
$main_comment_id = "".$rows_comment['id']."";
$main_comment_mem_id = "".$rows_comment['mem_id']."";
$main_commented_id = "".$rows_comment['commented_id']."";
$main_comment = "".$rows_comment['comment']."";
// Get user information
$sql_member_data = msql_query("SELECT * FROM users WHERE id ='$main_comment_mem_id'");
while($row2=msql_fetchj_array($sql_member_data)) {
$user_id = "".$row2['id']."";
$user_name = "".$row2['member_name']."";
}
// Display comment
echo "<b>$user_name</b><br>$main_comment";
// Search for any sub-comments
$sql_subcomments = "SELECT * FROM comments WHERE sub_commented_id='$main_comment_id' AND type='sub'";
while($row_subcomment=msql_fetchj_array($sql_subcomments)) {
// Get sub comment information
$subcomment_id = "".$row_subcomment['id']."";
$sucomment_mem_id = "".$row_subcomment['mem_id']."";
$subcomment_comment = "".$row_subcomment['comment']."";
// Get sub commenter information
$sql_member_data_sub = msql_query("SELECT * FROM users WHERE id ='$subcomment_mem_id'");
while($row2_sub=msql_fetchj_array($sql_member_data_sub)) {
$user_id_sub = "".$row2_sub['id']."";
$user_name_sub = "".$row2_sub['member_name']."";
}
// Echo sub comment
echo "<div style='margin-left: 20px;'><b>$user_name_sub</b><br>$subcomment_comment</div>";
}
}
?>