Build string to be used externally in while loop - php

i am looking to build a string while inside a mysqli while loop. i have a pages table that contains all the pages for my webapp however it has come to seo friendly these urls
my table looks like this
ID | Page Slug | Parent
------------------------
1 | Accounts | 0
2 | Customers | 1
3 | Details | 2
say i call the below function WebApp_UOC_Construct_Url(3) with the page id 3 it will echo out a url with one that looks like the below
/accounts/customers/details
this url is how i want it to be passed to my menus so i know the function is working however this will only echo on the page i need it to return a value inside the href and so if i change the echo to return and call the function like this
<a hef='" . WebApp_UOC_Construct_Url(3) . "'>Customer Details</a>
i only receive the last entry in the database so the url will look like this
/details
what other possible methods are there of creating the above
function WebApp_UOC_Construct_Url($page_id)
{
global $webapp_db;
$get_URI = mysqli_query($webapp_db,
"SELECT * FROM wa_sys_navigation WHERE wa_nav_id = '" . $page_id . "' ");
while($found_URI = mysqli_fetch_array($get_URI))
{
WebApp_UOC_Construct_Url($found_URI['wa_nav_parent']);
echo "/" . $found_URI['wa_nav_slug'];
}
}

If you change only echo to return you will get only the top level function call result. You need summarize the recursive calls results.
Example:
$parent = WebApp_UOC_Construct_Url($found_URI['wa_nav_parent']);
return $parnet . "/" . $found_URI['wa_nav_slug'];

Related

Link Product Attribute with multiple values - Woocommere

On my custom Single product page, I want to link every value of pa_ontwerper to the correct page. I used this code what works if pa_ontwerper has only 1 value:
$ont = $product->get_attribute('pa_ontwerper');
echo "<a href='https://www.website.nl/attribute" . $ont . "/'>" . $ont . "</a>" ;
This results in: designer1 with link to www.website.nl/attribute/designer1/
But when pa_ontwerper has for example 2 values, the output wil show something like:
designer1,designer2 with a link to www.website.nl/attribute/designer1,designer2/
Which is wrong.
What can I change in the code to make sure both values are linked correcly?

getting only certain entries of the database

The code bellow gets a few domains from my database and outputs them in a dropdown list. I need some help as I can't get this change i want to make work.
I want to show only the domains where in the database premium_only is set to 6, nothing else. If I try to change the if statement to $shortUrlDomain['premium_only'] == 6 I can't see any domains appear in the list which has premium_only set to 6 in the database. Why?
Database layout:
id | domain | premium_only | status | date_created
---+-------------+--------------+---------+-------------------
1 | exaple.com | 0 | enabled | 2020-03-02 08:13:0
2 | exaple2.com | 6 | enabled | 2021-03-02 08:13:0
PHP code:
<div>
<label for="shortUrlDomain"><?php echo t("short_url_domain", "Short Url Domain"); ?>:</label>
<select id="shortUrlDomain" name="shortUrlDomain" style="width: 100%;">
<?php
foreach ($shortUrlDomains AS $k => $shortUrlDomain) {
// active domains only
if ($shortUrlDomain['status'] != 'enabled') {
continue;
}
$lastPremiumOnly = $shortUrlDomain['premium_only'];
echo '<option value="' . (int)$k . '"';
// selected option
if ($k == (int)$_REQUEST['shortUrlDomain']) {
echo ' SELECTED';
}
echo '>';
'</option>';
}
echo '</optgroup>';
?>
</select>
</div>
// get base urls
$shortUrlDomains = getShortUrlDomains();
static function getShortUrlDomain($domainId)
{
// get base urls
$shortUrlDomains = getShortUrlDomains();
if(!isset($shortUrlDomains[$domainId]))
{
return _CONFIG_SITE_PROTOCOL.'://'._CONFIG_SITE_FULL_URL;
}
return _CONFIG_SITE_PROTOCOL.'://'.$shortUrlDomains[$domainId]['domain'];
}
It's a bit hacky, but you might add this statement earlier in your code.
Ideally you'd alter the database query further down the stack, but you question doesn't include any mention of how you set $shortUrlDomains.
<?php
// somewhere before you render the HTML as in the snippet from your Q.
/**
* This makes shortUrlDomains into a subset of the original array,
* showing only those who have a `premium_only` key with a value of `6`.
*/
$shortUrlDomains = array_filter($shortUrlDomains, function($domain) {
return (int) $domain['premium_only'] === 6;
});
See more of array_filter here, at the PHP docs.

Personalized content

I would like to, clicking on a link, showing a content personalized.
<div class="tab_event">
<?php
do{
echo'
<table class="preview_event">
<td class="date_event">
'.$row["Date"].'
</td>
<td class="title_event">
<p>'.$row["Title"].'<br></p>
Read more !
</td>
</table>
';
} while ($row = $query->fetch(PDO::FETCH_ASSOC));
?>
My program consist at doing a "preview" of some articles from the database.
And when you click on "read more" i would like that, automatically, you get a page where the all article is written.
But I don't have any ideas about how to do it, bcs i don't want to create a page manually for each article.
Does anyone have any idea about how to do ?
Thanks
To achieve what you want, you need to generate a href to another/same PHP powered page along with a query parameter with some value which uniquely identifies the article in your database.
Assuming your database table structure is similar to
| Id | Date | Title |
|----|------------|------------------------|
| 1 | 0000-00-00 | Some catchy post title |
| 2 | 0000-00-00 | Yet another post title |
| 3 | 0000-00-00 | Some other title? |
I will use $row["Id"] using the above table structure
<div class="tab_event">
<?php
do {
echo '
<table class="preview_event">
<td class="date_event">
'.$row["Date"].'
</td>
<td class="title_event">
<p>'.$row["Title"].'<br></p>
Read more !
</td>
</table>';
} while ($row = $query->fetch(PDO::FETCH_ASSOC));
?>
Now access the aid parameter on your linked script (showArticle.php) to fetch the article from databse
<?php
if (!empty($_GET['aid']) {
// escape and verify value of `$_GET['aid']`
// fetch and return article from database using the `$_GET['aid']` parameter
} else {
// tell the user no `Id` was provided
}
?>
It's quite an easy task, first of all, you need to modify your table page, and edit the read more link this :
Read more !
Where article_idis whatever record's identification you wanna use to store that information when clicking the link, for example the article's id field in the database, now you create a read.php page, which will change its content based on the article's id in the link, so you only need to create that and not one page for each article.
Then in read.php you retrieve that information from the URL so that you can query the database with the correct article's ID and show its content:
...
...
$article_id = $_GET['article'];
// then you query the database using that variable (using whatever method you prefer, this in just an example)
$query = $db->prepare("SELECT title, date, content FROM articles WHERE articles.id = " . $article_id);
$query->fetch(PDO::FETCH_ASSOC);
This way you can retrieve the single's article information and show them on the read.php page , creating only one page which changes its content based on which link you click.
Also, worth mention that you should remove that do...while, for and foreach loops are way more elegant.
One example would be (assuming that the variable containing the results is called $rows) :
<table> <!-- table tag outside of the loop, otherwise for each article a table is created, which is wrong -->
<tbody>
<?php
foreach($rows as $article) {
echo "<tr>"; // since we need one row for each article, we include the tr tag in the loop
echo "<td>".$article['title']."</td>";
echo "<td>".$article['date']."</td>";
echo "<td>".$article['author']."</td>";
...
...
echo "</tr>"; // and we close the row
}
?>
</tbody>
</table>

Store number in variable , add 1 then repeat

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);
?>

How to create conditional button in HTML based on MySQL data?

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" />';
}
?>

Categories