I am a newb with 6 months experience, self-taught via StackEx/books/etc. Created a pretty decent website with login/register and storing some info via mySQL. I have been through every single BLOB post here and I have some decent output.
I think I, like most newbs, know enough to be dangerous, don't have the greatest foundation laid out so when it gets to serious understanding of built-in functions, arrays and passing arguments we can lose the flow and I basically think I have dug a hole by using includes to call some navbar so that I can't just use a header to output the damn image as I echo the user name after login so it has already outputted lines and it will be a monster to undo. Three levels of nav, unauthenticated, authenticated and admin.
The database connection and write to/read from is OK. I can store the BLOB and I can even read it back and store the array in a variable and then debug see the binary but I can't get it to display on an HTML page.
Here is the fun:
$stmt = $dbc2->query("SELECT * FROM equip1");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$name = $row['equipname1'];
$desc = $row['equipdesc1'];
$img = $row['equipimg1'];
}
<div class="col-lg-12">
<h1><?php echo $name ?></h1>
<p><?php echo $desc ?></p>
<pre>
<?php print_r($img); ?>
</pre>
<?php echo "<img src='data:image/jpeg;base64," . base64_encode( $img ) . "' />"; ?>
</div>
$dbc2 connects
equip1 table is two simple varchar cols, one BLOB col.
$name and $desc echo out ok, the $img displays broken link.
I check the array via pre code and is matching, per what is stored in dB during upload.
From what I have read and gone through, seems like you can't do this at same time unless via data URI which I have done but still broken link. Not making sense to me at all. I try the header and of course output already started but I can see also outputs the binary.
Here is screenshot of the URI method:
Since both methods "seem" to get into and out of the dB but do not display I'm going round in circles. Please help me out to display the image on an html page. I would like to echo it anywhere and then I can just style the page after that. Thanks!
Try Like this
<?php echo '<img src="data:image/png;base64,'.base64_encode($img).'">';?>
Uhh NEVER MIND. Never thought reading someones answer would make for "fresheyes". I noticed I was encoding but I already encoded it when I stored it. Mea culpa. DOH! :)
Related
I'm working on a notification system and I want to use a kind of platform I've built for viewing the profiles with the ID's found in the database 'friendships' (which keeps track of the sender, recipient and date of a friendship request)
I've been trying to find a way to include this profile module (which uses a $_GET variable to fetch user information), for every row, and I need a way to set a variable for each row and then immediately include that module using the ID, but when I try this, only one appears instead of the 4 requests I have.
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='item friend'>";
$_GET['user'] = $row['sender_id'];
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/profile/module.php";
include_once($path);
echo "</div>";
}
I am aware that setting a variable like this is wrong, but I don't have a clue about how I would go about doing this.
UPDATE: After applying Gautum Rai's code, the correct about of divs are showing, but they appear empty.
This is the HTML that generates once I use the original code (which echoes the info of one person).
<div class="item friend">
<div class="profile">
<img class="pic" title="Taswell: Omikron Player" style="background-image: url("/img/badges/omikron.png") !important;" src="/uploads/profile19.jpg?" 2147213591="">
<p class="username">MyNameIsKnuckles<img class="medal" src="/img/medals/dragonfly.png" title="Queen Of The Hive"></p><br>
<p class="bio playing">nothin. just waiting for the next omikron build.</p>
</div>
Note: I already asked something similar to this, but I got comments telling me to join the variables or not use a while loop to fetch assoc. Please provide a relevant answer, I would really appreciate that.
You are susing include_once which is explicit including the file once, and your filename is always the same. Try only using include.
use file_get_contents() instead of include_once,
your code should be as :
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='item friend'>";
$_GET['user'] = $row['sender_id'];
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/profile/module.php";
file_get_contents($path); // want to print contents just echo it
echo "</div>";
}
There is an awful lot wrong here.
With the exception of $_SESSION, you should never write to superglobals.
You are calling include_once multiple times within a loop - this is wrong. I suspect you really want the content of the incldued file to be processed for each loop iteration - while include (or require) would suffice, this is also wrong. You are making very innefficient use of resources (and probably not separating your application logic and html). A much more correct way to solve the problem would be to define a function in your include file and call that with $row['sender_id'] as an argument.
So i am making a simple forum to learn some PHP with CodeIgniter, by simple i mean 1 page with posts and you can click on them to comment and view more info(Think of reddit). All the data is stored in a mySQL database. Anyways i got all the links to display on my page, but what i cant figure out is how to open a new page to show the description and comments of the post. I remember doing something similar with a long time ago, can't remember how i did that sadly.
<?php
foreach($records as $rec){
$test = $rec->PostName."<br/>";
Echo "<a href=#$test>$test</a>";
}
?>
<?php
echo '<div data-role="page" id="$test"></div>';
echo "THIS ISSSSS $test";
?>
So this is the part where i need help. Any suggestions are greatly appreciated
Well for starters you'll need to refactor your attempt at generating the links as that has issues as everyone has pointed out.
So I took the liberty to come up with some test code to demonstrate a few things.
So this is Part 1.
<?php
// Simulated Database results, an array of objects
//
// These may already be Slugs with First letter upper case, hyphenated between words
// But I will do that when generating the links.
$records = [
(object)['PostName'=>'Why I recommend reading more PHP Tutorials'],
(object)['PostName'=>'Why I should take heed of what others suggest and actually try them out before dismissing them'],
(object)['PostName'=>'What is the difference between using single and double quotes in strings'],
(object)['PostName'=>'Why everyone should know how to use their browsers View Source tool to inspect the generated HTML'],
];
foreach ( $records as $rec ) {
$test = $rec->PostName;
// This would make a good helper but Codeigniter might have something already.
// So you should go and read the manual.
echo "$test";
// The above uses " to wrap the string with escaped \" within the string
// cause you cant have both.
echo '<br>';
}
// This is only here for testing... I think.
echo '<div data-role="page" id="'.$test.'"></div>';
// The above uses single quotes to wrap the string.
echo "THIS ISSSSS $test";
Part of Part 2...
<?php
// Simulated Database results, an array of objects
//
//
// These may already be Slugs with First letter upper case, hyphenated between words
// But I will do that when generating the links.
$records = [
(object)[
'id'=>1,
'PostName'=>'Why I recommend reading more PHP Tutorials'],
(object)[
'id'=>2,
'PostName'=>'What is the difference between using single and double quotes in strings'],
];
foreach ( $records as $rec ) {
$name = $rec->PostName;
$id = $rec->id;
// The slug of the page isn't really being used here as
// we are providing the pages id for lookup via an AJAX Call.
echo "<a id=".$id." href=\"#".ucfirst(str_replace(' ','-',$name))."\">$name</a>";
echo '<br>';
}
// This is where the AJAX call fired by a click on an anchor, that sends the id
// which results in the fetching of the required details to plonk in our page-area div.
echo '<div class="page-area"></div>';
There are a number of ways to do this, but going totally basic, if we create a JS event for a click on an anchor.. Which fires an AJAX Call which posts the page id to our method that returns back the HTML of the results we want to display...
Sorry if I'm duplicating threads here, but I wasn't able to find an answer to this anywhere else on StackOverflow.
Basically what I'm trying to do is make a list in which variables entered in a form by a user can be kept. At the moment, I have the code which makes this possible, and functional, however the variables entered in the form only appear on the list after the user hits submit... As soon as I refresh the page or go to the page from somewhere else, the variables disappear. Is there any way I can stop this from happening?
Edit: here are the codes:
//Page 1
<?php
session_start();
$entries = array(
0 => $_POST['signup_username'],
1 => $_POST['signup_email'],
2 => $_POST['signup_city']);
$entries_unique = array_unique($entries);
$entries_unique_values = array_values($entries_unique);
echo "<a href='Page 2'>Link</a>";
$_SESSION['entries_unique_values'] = $entries_unique_values;
?>
//Page2
<?php
session_start();
$entries_unique_values = $_SESSION['entries_unique_values'];
foreach($entries_unique_values as $key => $value) {
$ValueReplace = $value;
echo "<br /><a href='http://example.com/members/?s=$ValueReplace'>" . $value . "</a><br/>";
}
?>
Your question is really quite vague. the answer depends on how much data you have to store, and fopr how long you need it to exsist.
By variable I assume you mean data the user has entered and that you want to put into a variable.
I also presume that the list of variables is created by php when the form is submitted.
Php will only create the variable list when the form is submitted as php is done entirely on the server, therefore you will not have or see the variables until the form is submitted.
if you wanted to be able to see the list as it is being created you could use javascript then once you have you php variables the javascript list isn't necesary.
each time you request a php page wheather it is the same one or not the server generates a totally new page, meaning all unhardcoded variables from previous pages will be lost unless you continually post the variables around the pages the server will have no memory of them.
You have a few viable options.
) keep passing the user created variables in POST or GET requests so each page has the necesary info to work with. Depending on the situation it might or might not be a good idea. If the data only needs to exsits for one or two pages then it is ok, but bad if you need the data to be accessable from any page on your web.
2.) start a session and store the variables in a session. Good if the data only needs to be around while the user is connected to the site. but will be lost if user close window or after a time.
3.) place a cookie. not a good idea but ok for simple data.
4.) create a mysql database and drop the variable info in there. great for permanent data. this is how i always complex user data.
just a few ideas for you to look into as it is difficult to see what you really mean. good luck.
use PHP session or store variable values in Cookies via JS or using PHP. It would be nice if you show your working codes :)
Your idea is fine, however you just need to add a little condition to your Page 1 that only set your SESSION values when POST is made, that way it will keep the values even if you refresh. Otherwise when you visit the page without a POST those values will be overwritten by blank values, which is what you are seeing now. You can modify it like
<?php
session_start();
if(isset($_POST["signup_username"]))
{
$entries = array(
0 => $_POST['signup_username'],
1 => $_POST['signup_email'],
2 => $_POST['signup_city']);
$entries_unique = array_unique($entries);
$entries_unique_values = array_values($entries_unique);
$_SESSION['entries_unique_values'] = $entries_unique_values;
}
echo "<a href='http://localhost/Calculator/form2.1.php'>Link</a>";
?>
You could use JavaScript and HTML5 local storage.
I asked the same question earlier and it got down voted and I have no idea why. I'm building a class that outputs a news feed, but it's a very structured html that I'm going to use a lot on the site (hundreds of times), so I'm using a class method to display the feed html and everything. And I just echo the whole thing.
The method is set up this way:
private function feed ($var)
{
$Statement = $this->Database->prepare("SELECT * FROM feed WHERE col = ? ORDER BY timestamp DESC LIMIT 50");
$Statement->execute(array($var));
while ($row = $Statement->fetch(PDO::FETCH_ASSOC))
{
echo ' <div class="feed-box">
' . /*facebook name retrieved from the facebook */ . '
' . $row["post"] . '
<br/>
</div>';
}
} //end feed
The class is set up so that another method that has more of the html template calls this feed method. (I'm not trying to be too redundant here, but again the last time I asked this question it got down voted). So I'm pretty new to oop, and I'm looking to display the profile pictures of people who are logged in with facebook. This isn't necessarily a facebook question because I know how to do it normally, but I don't know how to get the facebook information within the class scope using just their id. Normally I;d get their picture by going linking to this url https://graph.facebook.com//picture. How do I do this within the class when I only sotre their facebook id in the database? I've been working on this problem for a couple days now, and I can't figure it out on my own. So I really appreciate your help.
It's getting to the point where I just want to statically type everything out because I know I could easily do that, but I'd really love to learn what the proper way is, especially since editing so many of these little boxes if I want to change something one day would be a real hassle. Thanks for your help. I really appreciate it. Let me know if anything is unclear I'll update the questino as soon as I can.
How do I do this within the class when I only sotre their facebook id in the database
echo '<img src="https://graph.facebook.com/' . $row['facebook_id'] . '/picture" />' ;
You say you only store the facebook id in your database. That is all you need to display the picture. Just append /picture to the user's FB graph link to get the picture.
I'm working on a page where I've listed some entries from a database. Although, because the width of the page is too small to fit more on it (I'm one of those people that wants it to look good on all resolutions), I'm basically only going to be able to fit one row of text on the main page.
So, I've thought of one simple idea - which is to link these database entries to a new page which would contain the information about an entry. The problem is that I actually don't know how to go about doing this. What I can't figure out is how I use the PHP code to link to a new page without using any new documents, but rather just gets information from the database onto a new page. This is probably really basic stuff, but I really can't figure this out. And my explanation was probably a bit complicated.
Here is an example of what I basically want to accomplish:
http://vgmdb.net/db/collection.php?do=browse<r=A&field=&perpage=30
They are not using new documents for every user, they are taking it from the database. Which is exactly what I want to do. Again, this is probably a really simple process, but I'm so new to SQL and PHP coding, so go easy on me, heh.
Thanks!
<?php
// if it is a user page requested
if ($_GET['page'] == 'user') {
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
// db call to display user WHERE id = $_GET['id']
$t = mysql_fetch_assoc( SELECT_QUERY );
echo '<h1>' . $t['title'] . '</h1>';
echo '<p>' . $t['text'] . '</p>';
} else {
echo "There isn't such a user".
}
}
// normal page logic goes here
else {
// list entries with links to them
while ($t = mysql_fetch_assoc( SELECT_QUERY )) {
echo '<a href="/index.php?page=user&id='. $t['id'] .'">';
echo $t['title'] . '</a><br />';
}
}
?>
And your links should look like: /index.php?page=user&id=56
Note: You can place your whole user page logic into a new file, like user.php, and include it from the index.php, if it turns out that it it a user page request.
Nisto, it sounds like you have some PHP output issues to contend with first. But the link you included had some code in addition to just a query that allows it to be sorted alphabetically, etc.
This could help you accomplish that task:
www.datatables.net
In a nutshell, you use PHP to dynamically build a table in proper table format. Then you apply datatables via Jquery which will automatically style, sort, filter, and order the table according to the instructions you give it. That's how they get so much data into the screen and page it without reloading the page.
Good luck.
Are you referring to creating pagination links? E.g.:
If so, then try Pagination - what it is and how to do it for a good walkthrough of how to paginate database table rows using PHP.