Im trying to create a crumb trail for a custom web script that drills down three levels into a link system. there are not many pages so my idea seem less work than to integrate someone else's script !
this is how i mark or identify the page in the header of each page
first level page
<?php $page = 'index_week'; ?>
second level page
<?php $page = 'index_week_list'; ?>
third level page which is also the details page and no deeper levels exist after this
<?php $page = 'index_week_ details'; ?>
My questions are...
1-how do i give each of these a label to that i can show them in the crumb trail and not show ""index_week_ details""
2-how do i link the file name to go back to a previous level with the identifier i used so that it picks up the listing id i used to filter that page's content form the database?
Below the header are and beginning of each page to see my dilemma !
the first lever page
<?php
include("inc_login_config.php");
include("inc_gen_constants.php");
include("inc_meta_header.php");
require("inc_dbsql.php");
include("inc_link_sql.php");
?>
<?php $page = 'index_week'; ?>
</head>
<body marginwidth="0" marginheight="0">
<!-- End of all general inclusuions -->
<?php
$db = new LinkSQL($dbname);
$homecataresult = $db->getchildcatalog(0);
$table="linkexcatalog";
mysql_connect ($dbhost,$dbuser,$dbpassword);
#mysql_select_db ($dbname);
$result = mysql_query("select catalogid,catalogname,parentid from $table where arentid='0' order by priority asc" );
$num_fields = mysql_num_fields($result);
$num_rows = mysql_num_rows($result);
$row_cnt = 0;
while ($num_rows>$row_cnt)
{
$record = #mysql_fetch_row($result);
?>
<?php print "$record[1]"; ?><br>
<?php $row_cnt++;} ?>
<?php mysql_close(); ?>
The other Two PHP files looks much the same except i have different LEVEL page id in them ?
I hope i don't confuse to much !
Ta any help appreciated.
Break up your string (into an array) on _'s, then loop on that array to create the breadcrumbs.
Here's a quick example:
<?php
$me = 'index_week_details';
//Break into an array
$parts = explode('_', $me);
//Debug
//print_r($parts);
//Create breadcrumbs
$path = '';
foreach($parts as $part) {
$path .= $part . '_';
echo '' . $part . '' . "\n";
}
You can play with it live here:
http://codepad.org/dFpujKZ8
Input:
index_week_details
Output:
index
week
details
Related
I'm trying to make a fun side project and have come to a stop on a small problem. I am trying to have the echo command placed inside of the while and mysqli_fetch_assoc command as the data changes everytime the page refreshes, from the select from table random function.
I select random items like this:
$survivor_set = random_survivor();
$item_set = random_item();
$firstaidkitaddon_set = random_firstaidkitaddon();
$flashlightaddon_set = random_flashlightaddon();
$keyaddon_set = random_keyaddon();
$mapaddon_set = random_mapaddon();
$toolboxaddon_set = random_toolboxaddon();
$survivoroffering_set = random_survivoroffering();
The problem line of code looks like this:
<?php while($toolboxaddon = mysqli_fetch_assoc($toolboxaddon_set)) { ?>
However I need the words after $ to be changed so was looking for something like this to work:
<?php while($ echo h($item['item']);addon = mysqli_fetch_assoc($ echo h($item['type']);addon_set)) { ?>
This is probably explained poorly, I would appreciate if anyone could lend some time to help me where I can show in more details what exactly I am trying to accomplish.
The code shows the random item that was selected from the table.
<?php while($item = mysqli_fetch_assoc($item_set)) { ?>
<?php echo h($item['name']);?> E.g. Engineers Toolbox
<?php echo h($item['rarity']);?> E.g. veryrare
<?php echo h($item['type']);?> E.g. toolbox
<?php echo h($item['media']);?> E.g. engineerstoolbox.png
<?php } ?>
I am then trying to find a way to put the echo h($item['type']) into the next output. so it would look like this when it is a toolbox.
<?php while($toolbox = mysqli_fetch_assoc($toolbox_set)) { ?>
But then could change due to it being a different item type that was pulled:
<?php while($flashlight = mysqli_fetch_assoc($flashlight_set)) { ?>
Full code, for context: https://zerobin.net/?9f772676aa87df3f#Gxy43WGqShTkL/VG42+t3nT4+sxGhxFy+GDB0B3+YH0=
You should store all your different add-on results in an associative array where the keys match the possible "type" values from your main item query. This allows you to select an item from the array using a string to reference its key - and you can take that string value from your $item['type'] variable.
e.g.
$survivor_set = random_survivor();
$item_set = random_item();
$addons = array(
"firstaid" => random_firstaidkitaddon(),
"flashlight" => = random_flashlightaddon(),
"key" = random_keyaddon(),
"map" = random_mapaddon(),
"toolbox" => random_toolboxaddon()
);
$survivoroffering_set = random_survivoroffering();
Then later on you can select the right set of results from the associative array by selecting the index which matches $item['type']:
<div class="itemaddonscontainer">
<div class="<?php echo h($item['type']);?>">
<?php while($addon = mysqli_fetch_assoc($addons[$item['type']])) { ?>
<img class="<?php echo h($addon['rarity']);?> survivor-item" src="imgs/survivor/itemaddon/<?php echo h($addon['media']);?>"/>
<?php } ?>
</div>
</div>
N.B. mysqli_free_result probably isn't necessary here.
Hey so I am currently working on an html/php page.
I have a header file that I am supposed to include in my page but for some reason it's not working and I have no idea, I've done the same thing for all my other pages and it has worked so I don't know why all of a sudden its not working.
<?php
include 'header.php';
?>
<center>
<p>
This page utilizes several postgreSQL method calls. Such as pg_connect(),
pg_query(), and pg_fetch_result().
</p>
<!-- setup the table -->
<table border="1" width="75%">
<tr><th width="50%">Make</th><th width="15%">Model</th><th width="20%">Year</th><th width="15%">MSRP</th></tr>
<?php
$output = ""; //Set up a variable to store the output of the loop
//connect
$conn = pg_connect("host=127.0.0.1 dbname=slotegraafd_db user=slotegraafd password=100658347" );
//issue the query
$sql = "SELECT automobiles.make, automobiles.model, automobiles.year, automobiles.msrp
FROM automobiles
ORDER BY automobiles.year ASC";
$result = pg_query($conn, $sql);
$records = pg_num_rows($result);
//generate the table
for($i = 0; $i < $records; $i++){ //loop through all of the retrieved records and add to the output variable
$output .= "\n\t<tr>\n\t\t<td>".pg_fetch_result($result, $i, "Make")."</td>";
$output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Model")."</td>";
$output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Year")."</td>";
$output .= "\n\t\t<td>".pg_fetch_result($result, $i, "msrp")."</td>\n\t</tr>";
}
echo $output; //display the output
?>
</table>
<!-- end the table -->
</center>
</body>
</html>
This is my full set of code with the include statement. There is no opening html or body tag because it is in the header file therefore I am not supposed to add it in this page.
Anyway any help?
Thanks.
Make sure that the file path is correct. If this new file is in a folder, then you need to change your include statement.
For example, if this new file is in a folder called foo, you could say: include '/foo/header.php
Or you can use a relative path: include ../header.php
Read up on file paths if needed.
It should work just the way your code looks like. Maybe your path of header.php isnt correct, or the content can't be displayed / executed so it shows nothing.
I've been playing around with a php input function that will build a html page for a game, something like a wiki. All is going fine and dandy but when trying to build an array into a string it's passing back some funny errors.
It's happening to every one that involves an array and I've snipped some code out that works independently to save me time, but just can't figure it out. I'm using the Linux Terminal to run the script and when I set it to echo inside the foreach loop it does it just fine, it just won't when I try building it into a HTML file.
Here is the input script:
echo "\nHow many ranks were there (number)?:\n";
$facInputRankLimit = readline();
echo "Please read carefully and supply the ranks in descending order (HIGHEST > LOWEST):\n";
$facInputRankCount = 0;
$facInputRankString = "";
while ($facInputRankCount < $facInputRankLimit) {
echo "Enter a rank:\n";
$facInputRanks[$facInputRankCount] = readline();
$facInputRankCount++;
}
foreach ($facInputRanks as $facInputRankList) {
$facInputRankString .= $facInputRankList.PHP_EOL;
}
Then I'll build it into a multi-line echo (rather than appending every single block of code):
$facBuildPage = <<<EOT
<?php
\$facRankLimit = '$facInputRankLimit';
\$facRanks = '$facInputRankString';
include('faction2.html');
?>
EOT;
The variables with the backslashes will then be built into "$facFileName.php" and will be a set of variables inputted through this script (input2.php), which will each also include the same html page.
To top it off I'm getting really weird results... if I create 5 ranks, each as "1 2 3 4 5", I actually get "1 2 3" with two vertical linebreaks in between.
Edit: Snipped some of it, I didn't want to risk not adding enough info but it turns out I added a bit much.
For instance, the input I'm giving is:
$facInputRankLimit = 5;
$facInputRanks[1] = 1; -> $facInputRanks[5] = 5;
But it prints:
1<br><br>2<br><br>3
#Deepkak - The relative code in faction2.html is:
<div class="fpDivisions fpBox">
<span class="header">Ranks</span><br>
<?php
$facRankCount = 0;
while ($facRankCount < $facRankLimit) {
echo $facRanks[$facRankCount].'<br>';
$facRankCount++;
}
?>
</div>
I Hope this will solve the issue
<?php
echo "\nHow many ranks were there (number)?:\n";
$facInputRankLimit = readline();
echo "Please read carefully and supply the ranks in descending order (HIGHEST > LOWEST):\n";
$facInputRankCount = 0;
while ($facInputRankCount < $facInputRankLimit) {
echo "Enter a rank:\n";
$facInputRanks[$facInputRankCount] = readline();
$facInputRankCount++;
}
$array = [];
foreach ($facInputRanks as $facInputRankList) {
$array[] = "'".$facInputRankList."'";
}
$facInputRankString = "[".implode(",",$array)."]";
$facBuildPage = <<<EOT
<?php
\$facRankLimit = '$facInputRankLimit';
\$facRanks = $facInputRankString;
include('faction2.html');
?>
EOT;
echo $facBuildPage;
I've created a workaround for this, incase anyone needs any help in the future. It was as simple as echoing it from the HTML page, rather than creating a loop that runs through both the PHP script and the HTML code.
PHP input code:
echo "\nHow many ranks were there (number)?:\n";
$facInputRankLimit = readline();
echo "Please read carefully and supply the ranks in descending order (HIGHEST > LOWEST):\n";
$facInputRankCount = 0;
while ($facInputRankCount < $facInputRankLimit) {
echo "Enter a rank:\n";
$facInputRanks[$facInputRankCount] = readline();
$facInputRankCount++;
}
$facInputList = implode("<br>", $facInputRanks);
PHP output code:
$facBuildPage = <<<EOT
<?php
\$facRanks = '$facInputList';
include('faction2.html');
?>
EOT;
file_put_contents("test/$facFileName.php", $facBuildPage);
HTML output code:
<div class="fpDivisions fpBox">
<span class="header">Ranks</span><br>
<?php echo $facRanks; ?>
</div>
I have a project I am working on, where I scan a directory and pull all of the images from the directory that will display the gallery images. Problem is, some galleries have 300+ images and this is causing a lag. I have looked around, but have not found a proper way of getting my code to paginate. Here is my code snippet of where I am running the foreach loop and getting each image source from the directory:
/** get the model's gallery
===================================================== **/
if(isset($_GET['count'])){
$photo_count = $_GET['count'];
}
if(isset($_GET['model_dir'])){
$model_dir = $_GET['model_dir'];
/** get the higres gallery images
===================================================== **/
$root_directory = $_SERVER['DOCUMENT_ROOT'];
$photo_directory = "{$root_directory}/members/content/model_gallery/{$model_dir}";
$gallery_list = scandir($photo_directory);
$gallery_list = array_diff($gallery_list, array('.', '..'));
foreach($gallery_list as $gallery){
echo "
<a href='{$index_url}join.php'>
<img alt='' src='{$site_url}members/content/model_gallery/{$model_dir}/{$gallery}'
data-image='{$site_url}members/content/model_gallery/{$model_dir}/{$gallery}'
data-description=''>
</a>
";
}
}
Any ideas on how I could go about paginating this code would be greatly appreciated.
I solved the problem myself. For those who had something tangible to respond with, thanks for your help. For others who just wanted to critique code, not knowing what the full purpose of the code is and whether or not it is the final piece of code, should probably keep your input to yourself.
/** get the model's gallery
===================================================== **/
if(isset($_GET['count'])){
$photo_count = mysqli_real_escape_string($_GET['count']);
}
if(isset($_GET['model_dir'])){
$model_dir = mysqli_real_escape_string($_GET['model_dir']);
/** get the higres gallery images
===================================================== **/
$root_directory = $_SERVER['DOCUMENT_ROOT'];
$photo_directory = "{$root_directory}/members/content/model_gallery/{$model_dir}";
$gallery_list = scandir($photo_directory);
$gallery_list = array_diff($gallery_list, array('.', '..'));
$photo_count = $photo_count;
$gallery_limit = 35;
$qty_pages = ceil($photo_count / $gallery_limit);
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }
$start_from = $page * $gallery_limit - $gallery_limit ;
$gallery_list = array_slice($gallery_list,$start_from,$gallery_limit);
echo "<div id='gallery'>";
foreach($gallery_list as $gallery){
echo "
<a href='{$index_url}join.php'>
<img alt='' src='{$site_url}members/content/model_gallery/{$model_dir}/{$gallery}'
data-image='{$site_url}members/content/model_gallery/{$model_dir}/{$gallery}'
data-description=''>
</a>
";
}
echo "</div>";
echo "<div class='contain_pagination'>";
if($photo_count != 0){
echo "<nav class='contain_model_nav'><ul class='pagination'>";
for ($i = 1; $i <= $qty_pages; $i++){
echo "<li><a href='{$index_url}pages.php?id=gallerytour&model_dir={$model_dir}&count={$photo_count}&total={$total_photo_count}&page={$i}'></a>";
echo "</ul></nav>";
}
}
echo "</div>";
}
?>
<script type="text/javascript">
$(document).ready(function(){
$('.pagination').pagination({
items: <?php echo $photo_count;?>,
itemsOnPage: <?php echo $gallery_limit;?>,
cssStyle: 'light-theme',
currentPage: <?php echo $page;?>,
hrefTextPrefix: '<?php echo $index_url ?>pages.php?id=gallerytour&model_dir=<?php echo $model_dir; ?>&count=<?php echo $photo_count; ?>&total=<?php echo $total_photo_count; ?>&page='
});
});
</script>
That is how I was able to paginate the foreach loop. I used nogad's idea for the array_slice function and then I used this open source for the pagination functionality:
http://flaviusmatis.github.io/simplePagination.js/
From there I made sure I had the $photo_count, which contains the count of all images within the directory and essentially what I would be paginating through.
I then set my $gallery_limit per page, which was 35. From there, I determined the number of pages with $qty_pages and dividing by the total $photo_count / and the set $gallery_limit
Next I check if the page isset if it is, then $page is == to the $_GET[page] else it is $page is == 1.
Next I get the $start_from value by multiplying $page * $gallery_limit and then subtracting by $gallery_limit
Next I make the value for the $gallery_list which is an array = to array_slice($gallery_list,$start_from,$gallery_limit);
So my parameters within the function are the entire array of src images for the $gallery_list, the integer of where to start from, and the integer of the $gallery_limit, in this case I set it to a default of 35.
Next I check if $photo_count is != to 0 and then run my normal for loop. the parameters within the url for the anchor will be dependent obviously on your site. One thing to note, is to simply add the &page={$i} parameter to the end of your url.
Finally you use the pagination plugin to write a small jQuery script for the pagination functionality. You should be able to decipher what I did within the jQuery script. I hope this helps someone out, because I have looked at other answers, and none delivered a satisfied result. They were almost as moronic as people like OlegLoginov replying to your question.
header.php
<?php
$conn = mysql_connect('localhost', '-', '-');
#mysql_select_db('accmaker', $conn) or die("Unable to select database");
?>
<html>
<head>
<title>Mysite.com - <?php isset($pageTitle) ? $pageTitle : 'Home'; ?></title>
</head>
<body>
profile.php
require 'header.php';
$q = mysql_query("SELECT * FROM users WHERE username = '$username'");
$r = mysql_fetch_assoc($q);
$pageTitle = "Profile of $r[username]";
I think you understand what i want
I cant include header.php after the query, because i wont be connected to mysql
waht do you suggest other than having the connection snippet on every page
What do I suggest? A MVC (Model-View-Controller) Framework like Kohana. If you don't want to go that route, break your connection off into its own file:
<?php
# connect
require_once("connection.php");
# load page data array
require_once("page-data.php");
?>
...
<title><?php print $page["title"]; ?></title>
Note here how I have a $page array of data. This will be helpful when debugging later rather than having several independent variables. With an array of page data, I can quickly see all of the information laid out for any given page:
print "<pre>";
print_r($page);
print "</pre>";
Determining your title should be done within page-data.php, rather than on your page:
$config["site_name"] = "Bob's Shoe Mart";
$config["admin_email"] = "bob#shoemart.com";
/* query to get $row['title'] */
$page["title"] = (!empty($row["title"])) ? $row["title"] : $config["site_name"] ;
Not sure of a "best" solution, but we currently include multiple files. We have our "utilities.php" file that connects to the database and provides some nice functions. We then set our page titles and then we include "top.php" which is the layout portion. It doesn't have anything except HTML with a little bit of PHP for display purposes. Looks like this:
include "utilities.php";
$pageTitle = "Welcome";
include "top.php";