I have a dynamic menu and I want to send the user to a new page on click with some db values. I want to use a session here without forms or via $_GET.
Presently, using the code below, only the last value of the loop is appearing in the $_SESSION. Perhaps I need to use a 2D array here, but I am not sure what to do:
<?php
$i = $cit['city_id'];
$selectCity = $dbh->prepare("SELECT * FROM `table` where city_id=$i");
$selectCity->execute();
$cityNum = $selectCity->fetchAll();
foreach($cityNum as $cities)
{
$_SESSION['$centreNum'] = $cities['centre_id'];
?>
<li class="first"><?php echo ucwords($cities['centre_location']); echo $_SESSION['$centerNum']; ?></li>
<?php
}
$centre_num = $cities['centre_id'];
?>
You have a couple problems (the one I think you mean to do...) in your example:
...
$cityNum = $selectCity->fetchAll();
foreach($cityNum as $cities)
{
# Not sure if you are trying keep this a variable key or not.
# | Make this session an array, you keep overwriting variable
# | |
# | |
# v------+---v v
$_SESSION['$centreNum'][] = $cities['centre_id'];
?>
...continue
Related
i'm trying to adjust an existing Web application, which i didn't programm. This is of the it's functionalities which the user using this Web App aren't satisfied with. So far i've managed to do what it's supposed to do, but my problem is that when i add new customer with the number bellow and try to edit an old entry i only get the last entry which has been added. I've tried using $this. but i doesn't help. So my question is how can i get the php to look at the entry coresponding to the customer's id and not only looking at the last entry added.
//set the query and fetch data from it
$resnew = mysql_query("SELECT * FROM orders");
while ($dnew=mysql_fetch_assoc($resnew)) {
$number = $d['number'];
$auto_id = $d['auto_id'];
$modell = $d['modell'];
}
//set the other query to fetch data from it
$reslevel=mysql_query("SELECT level FROM modells WHERE auto_id=$modell");
while ($dlevel=mysql_fetch_assoc($reslevel)) {
$parameter="level";
$$parameter=$dlevel["$parameter"];
}
?>
<li> //Output
<label><? echo $label ?>:</label>
<input type="text"
name="<? echo $settings_id ?>_settings_one_rule"
maxlength="<? echo $lenght ?>"
class="textfeld"
style="width:<? echo $should_lenght ?>px;"
value="<?
if ($settings_id == 12) {
echo "$number . $level ;
}
?>"
>
</li>
Here's your logic at the moment:
Read data part 1 into a variable V
Read data part 2 into a variable V
...
Read data part n into a variable V
Print variable V
Clearly, you're only going to get the last part.
Move your output inside the loop.
Then your logic will be:
Read data part 1 into a variable V
Print variable V
Read data part 2 into a variable V
Print variable V
...
Read data part n into a variable V
Print variable V
I'm work on PHP CMS , almost done of it
I use the Material Design Lite pop-up share menu, this required a html id property
as ex. if I post 2 articles and share menu has same id number then, when user press on a one of them the 2 will appear
so I need PHP code that do this for me, I don't need a very difficult script
just need a script do this :
1 - make a variable contains id = 0
2 - add 1
3 - store the new value , id = 1
4 - repeat for every post
I tried this code :
<?php
$id = 0;
echo $id += 1 ;
$id = $id ;
?>
thanks in advance
If you want global counter you can save it to filesystem:
<?php
$idstr = file_get_contents("counter.txt");
$id = intval($idstr)+1;
file_put_contents("counter.txt", $id);
echo($id);
?>
Is there any way to create a new a href that remembers all the submitted data earlier. I don't know how to say it properly so I will describe in the code:
The first button
Click me
After the user clicks it, he is redirected to the same page but with the new button :
Click me
And so on :
Click me
How do i create the n variable to be added and increased after the button is pressed ?
( I have a table displayed from a database by using the while command with mysqli_fetch_array($database); )
The table is created like (trivial ) :
$retrieve_items = mysql_query("SELECT * FROM items WHERE id > 0");
$col = 0;
echo '<table width=100% border= 1><tr>';
while($row = mysql_fetch_array( $retrieve_items )) {
$col ++;
echo '<td>'.$row['name_item'].'</td>';
if ($col % 5 == 0 )
{
echo '</tr><tr>';
}
}
echo '</tr></table>';
I recommend not to try managing numbered variable names. If there is no important reason to do so, it will make your logic unnecessarily complicated.
PHP understands array parameters in e.g. $_GET. They are passed from HTML with empty braces appended to the parameter name.
This is a little demo to illustrate this alternative approach:
<?php
// get the passed array or generate a new one
$n = isset($_GET['n']) ? (array) $_GET['n'] : [];
//ppend 2 random numbers
$n[] = rand(1,100);
$n[] = rand(1,100);
//output the link with GET parameters in query
?>
the link
<!-- or let PHP's built-in generate a propper query --><br>
the link
Be aware, that the second link generated by http_build_query contains indexes of the array, which are commonly based on 0.
in php, to access the current query String, you can use
$_SERVER['QUERY_STRING']
so you could so something like
Click me
or you could replace your previous value in the query string
I am trying to create a button that will either say "Follow" or "Unfollow", depending on whether or not the current user follows another user.
If John followed Tim, but not Sarah, the web view would look as follows, according to John's view:
_________________________________
| | |
| Tim | (unfollow) |
|________________|______________|
| | |
| Sarah | (follow) |
|________________|______________|
Where (" ") denotes a button.
I have a database that indicates who follows whom, but how would I display the correct button based upon validation with said database?
Assuming you have three fields "name_id","name" and "followed" where "name_id" is the id of the person, "name" is a string signifying the name of the person, and "followed" is a boolean:
<script type="text/javascript">
function toggleFollowing(name_id) {
window.location = 'toggleFollowing.php?name_id='+name_id;
}
</script>
...
<?php
...
while ($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>'.$row['name'].'</td><td><a href=""><button type="button" onclick="toggleFollowing('.$row['name_id'].')">'.($row['followed']==1 ? 'Unfollow':'Follow').'</button></td>';
echo '</tr>';
}
...
?>
You would have toggleFollowing.php receive the variable $_GET['name_id'] to toggle on the database and come back to this page. I'm assuming you have the current user's ID stored as a session variable or by other means since you would need that as a primary reference to update the record. If you're passing that from page to page by some other means, then you would want to pass that variable as well.
Apparently, this is more truncated code, but a better method would be to use AJAX to perform the toggling on the DB, and DOM manipulation (JQuery?) for a "real-time" update.
Hard to answer without examples of your code, but something like this?
<?php
if(follow){
echo '<input type="button" value="Follow" />';
} else {
echo '<input type="button" value="Unfollow" />';
}
?>
Hi i have a sorting question to ask.
My database has images with "Photoname" as one of the columns in mySQL table.
Database:
Photoname
Apple
Bear
Cat
Orange
So in my catalog.php, I would display all the images from the database. Then, i have a navigation bar(div) to allow viewers to filter the images. So users can view by A | B | C... | Z|.
My question is with regards to the URL and approarch. I would create imagesort.php which would handle all the mySQL in a general way( so that i don't have to waste time creating for A - Z).
Should the imagesort.php be something like imagesort.php?sort=A? Then in imagesort.php how can i get the value A? For example:
select photoname, date from image where photoname LIKE 'a%'
And also if the above way is correct, how can i parse this variable A to the link in catalog.php?
Here is what i've done so far in my catalog.php:
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
...
</ul>
Do i really have to do this for 26 entries? or is there a simpler method?
You can use something like this...
<ul>
<?php for($i=65;$i<90;$i++) { ?>
<li>&#<php echo $i;?>;</li>
<?php } ?>
</ul>
You'll need to get the value of sort from the $_GET array. Make sure you sanitize your input before using it in your SQL query.
$match = "";
if(isset($_GET['sort']))
{
// Escape the string to guard against SQL injection
$match = mysql_real_escape_string($_GET['sort']);
}
$query = "select photoname, date from image where photoname LIKE '$match%'";
This code has the effect of working for any value of sort, including an empty string. If you want to limit the valid strings to a single uppercase alpha character, you'll have to check separately for that, like so:
if(strlen($match) != 1 || ctype_upper($match) == false)
{
$match = "";
// Maybe some other error condition
}
If the links are formatted like that then they will end up in the $_GET super global:
$sort = $_GET['sort'];
For the second question, you can easily use a loop in php to run through the letters of the alphabet, to generate the pagination content you need:
$link_base = 'imagesort.php';
$pagination_content = '<ul>';
for($i=65; $i<=90; $i++)
{
$pagination_content .= '<li>'.chr($i).'</li>';
}