Hey iam making a slot machine and is almost done. The only thing i need is the credit's to stay so it just add points on the credit's. Like if i have 100 credits and then get 25 credits i want it to say 125 credits. Now i don't know how to get the credits from the round before.
This is what i got:
<?
$tal = rand (1,3 ); {
echo "<img src='css/billeder/enarmet$tal.gif' class=billed />";
$tal2 = rand (1,3 );
echo "<img src='css/billeder/enarmet$tal2.gif' class=billed />";
$tal3 = rand (1,3 );
echo "<img src='css/billeder/enarmet$tal3.gif' class=billed />"; }
?>
</div>
<div id="credits">
<h3 id="credits2">CREDITS</h3>
<h3 id="credits3"><?php
$credits=$_GET['credits'];
if ($tal . $tal2 . $tal3 == 111){
($credits=($credits+100));
}
if ($tal . $tal2 . $tal3 == 222){
($credits=($credits+50));
}
if ($tal . $tal2 . $tal3 == 333){
($credits=($credits+25));
}
echo $credits;
?></h3>
</div>
</div>
<form action="index.php" method="POST">
<input type="submit" value="SPIN" class="knap">
</form>
<form action="cashout.php" method="POST">
<input type="submit" value="CASH OUT" class="knap">
</form>
</div>
What about using sessions? Store and retrieve credits from session storage using
session_start();
$credits = $_SESSION['credits'];
at the top of your script and
$_SESSION['credits'] = $credits;
at the bottom.
This way credits will be preserved between page-loads.
You can remove the curly braces at the end of the following lines, its not need and try it.
<?
$tal = rand (1,3 ); { // remove the open brace
echo "<img src='css/billeder/enarmet$tal.gif' class=billed />";
...
echo "<img src='css/billeder/enarmet$tal3.gif' class=billed />"; } // remove the close brace
?>
Related
I have a set of image url's in a database which I am echoing into an <img>tag to display on a website.
I am using bootstrap and have my basic model set up like this:
<div class="row-fluid gallery">
<div class="span3">
<img src="some fancy url here for the image">
</div>
</div>
Now if you have ever used bootstrap you know once that span3 reaches 12 (basically when 4 images are displayed in the row). You must start the above all over to keep the images all in line.
I have a PHP script below that echoes out the image source and the above layout. The problem is, I have more than 4 images to echo out. I removed credentials for security purposes.
$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM gallery");
while($row = mysqli_fetch_array($result))
{
echo "<div class='row-fluid gallery'>";
echo "<div class='span3'>";
echo"<img src='". row['image_url']."'>";
echo "</div>";
echo "</div>";
}
mysqli_close($con);
My question is how do you do something like:
for every 4th image {
<div class="row-fluid gallery">
<div class="span3">
<img src="some fancy url here for the image">
</div>
</div>
}
Basically, I can say it in English and know what I need. But I can't say it in PHP and have it know what I need. Any help would be appreciated.
Create a counter variable outside the loop and check every fourth
$i = 1;
while($row = mysqli_fetch_array($result))
{
// do every time
if($i % 4 == 0)
{
// do only each 4th time
}
$i++;
}
$counter=0;
while($row = mysqli_fetch_array($result))
{
echo "<div class='row-fluid gallery'>";
echo "<div class='span3'>";
echo"<img src='". row['image_url']."'>";
echo "</div>";
echo "</div>";
$counter++;
if (!($counter%4))
// do your fancy staff, this is the forth image
}
I have an advertising website project where users can add their offers for different tourism related businesses. I created the dynamic generated page for each offer and I would like to add a comment box to it where visitors can post their opinions. The page is generated as it should, the link being advertisement.php?id=2 where the id is the id of that offer. When I click the submit button for the comment the page link changes to advertisement.php?advertisement_comment=&place_comment=Place+comment where advertisement_comment is the name of the comment box and place_comment is the name of the submit button. Because of this I get Notice: Undefined index: id in C:\xampp\htdocs\Project\advertisement.php on line 3. My id variable is equal to $_GET['id'] but since the page url changes it can no longer get it. Is there any way I can prevent the link from changing? If not maybe you can think of a different database structure and php code that I can use to get where I want because I'm kinda stuck. Please let me know if I need to edit my question in any way before downrating. Thank you very much!
Code for the generated page:
<?php
session_start();
$id = $_GET['id'];
require_once("includes/db_connect.php");
$query_dynamic_advertisement = "SELECT * FROM advertisings WHERE id = '$id'";
$result_dynamic_advertisement = mysql_query($query_dynamic_advertisement) or die (mysql_error());
$fetch_dynamic_advertisement = mysql_fetch_assoc($result_dynamic_advertisement);
// Hit counter
$query_advertising_views="UPDATE advertisings SET advertising_views = advertising_views + 1 WHERE id = '$id'";
$result_advertising_views=mysql_query($query_advertising_views) or die (mysql_error());
// Comments
if (isset($_POST['place_comment']))
{
$comment = mysql_real_escape_string($_POST[advertisement_comment]);
$advertising_id = $_SESSION['id'];
$query_place_comment = "INSERT INTO advertising_comments VALUES ('', '$comment', '$advertising_id')";
mysql_query($query_place_comment) or die (mysql_error());
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>Title</title>
</head>
<body>
<div id="wrapper">
</br>
<h3>Home</h3>
<img src="images/logo.png" alt="SporeDEV logo" id="logo">
</br>
<!-- Meniu -->
<ul>
<li>Signup</li>
<li>Login</li>
<li>Forgot Password</li>
<li>Logout</li>
<li>Advertising Panel</li>
<li>User Panel</li>
<li>User Profile</li>
<li>Admin Panel</li>
</ul>
<!-- Sfarsit de meniu -->
</br>
<?php
echo "<p> General information </p>";
echo "</br>";
echo "Location name: {$fetch_dynamic_advertisement['location_name']}";
echo "</br>";
echo "Location type: {$fetch_dynamic_advertisement['location_type']}";
echo "</br>";
echo "</br>";
echo "</br>";
echo "<p> Address </p>";
echo "</br>";
echo "Region: {$fetch_dynamic_advertisement['region']}";
echo "</br>";
echo "Settlement: {$fetch_dynamic_advertisement['settlement']}";
echo "</br>";
echo "Street: {$fetch_dynamic_advertisement['street']}";
echo "</br>";
echo "Street number: {$fetch_dynamic_advertisement['street_number']}";
echo "</br>";
echo "</br>";
echo "</br>";
echo "<p> Contact information </p>";
echo "</br>";
echo "E-mail: {$fetch_dynamic_advertisement['email']}";
echo "</br>";
echo "Phone: {$fetch_dynamic_advertisement['phone']}";
echo "</br>";
echo "Website: <a href={$fetch_dynamic_advertisement['website']} target=_blank>{$fetch_dynamic_advertisement['website']}</a>";
echo "</br>";
echo "</br>";
echo "</br>";
echo "<p> Statistics </p>";
echo "</br>";
echo "Added: {$fetch_dynamic_advertisement['add_date']}";
echo "</br>";
echo "Modified: {$fetch_dynamic_advertisement['last_modified']}";
echo "</br>";
echo "Advertising views: {$fetch_dynamic_advertisement['advertising_views']}";
echo "</br>";
echo "</br>";
echo "</br>";
echo "<p> Description </p>";
echo "</br>";
echo "Description: {$fetch_dynamic_advertisement['description']}";
echo "</br>";
echo "</br>";
echo "</br>";
echo "<p> Comments </p>";
echo "</br>";
?>
<!-- Comment form -->
<p> Add a comment </p>
</br>
<form action = "advertisement.php?id=<?php echo $id;?>" method = "$_POST">
<textarea name="advertisement_comment"></textarea>
</br>
<input type="submit" name="place_comment" class="button_1" value="Place comment">
</form>
</div>
</body>
</html>
The structure of the table advertising_comments is: id(int 11, autoincrement), comment (varchar 255), advertising_id (int 11, this should be the same as the id of the initial page).
I know that MySQL is depreciated. If you have the time please check this question as well: Simplest MySQL to MySQLi transition
Your URL is changing following the form submission because it's defaulting to the GET method. This is due to a typo in your form element.
Change this line:
<form action = "advertisement.php?id=<?php echo $id;?>" method = "$_POST">
to:
<form action="advertisement.php?id=<?php echo $id;?>" method="post">
This will allow your comment form to be submitted using POST.
Extra note, you should sanitize the all the user inputs, including the page ID to prevent SQL injection attacks. This is not safe:
$id = $_GET['id'];
Edit: the session_start() was in the "entire code of initial page," but wasn't showing. I needed another newline.
Edit 2: Showing the output of the raw HTML rather than the displayed page.
I am working on a document conversion site. It takes a one or two page .docx file and converts it to simple html. (Basically replacing newlines with p tags, no html or head tags as it will be displayed in an existing page). For test purposes I'm converting a .docx file with a single word - "one" - in it.
I am storing the converted data as a string in the variable $_SESSSION['doc']. Because of the issues I've been having, I'm also storing a $_SESSION['test'] string.
I do a var_dump($_SESSION) on the initial page and I can see both variables with populated strings.
I do a var_dump($_SESSION) on the following screen, and the $_SESSION['test'] string comes through okay, but the $_SESSION['doc'] is now a 0 length string. I'm not certain what's truncating my 'doc' string.
The relevant variables are $_SESSION['doc'], $_SESSION['test'], and $outputData. I included the entire first page at the end of this post since I suspect $outputData is getting overwritten.
I believe this is the relevant code snippet for the initial page:
<?php $outputData = preg_replace("/.+/", "<p class='converted'>$0</p>", $outputData);
$outputData = preg_replace("/<\/p>\n\n/", "<br /><br /></p>", $outputData); ?>
<?php if(isset($outputData)){$_SESSION['doc'] = $outputData;}else{$_SESSION['doc']="JustATest";} $_SESSION['test'] = "<>()!';!SDFSDFG^%$"; echo "\n<div><br /><br />var_dump: " . var_dump($_SESSION) . "</div>"; ?>
Output:
array(2) {
["doc"]=>
string(32) "<p class='converted'>one</p>
"
["test"]=>
string(18) "<>()!';!SDFSDFG^%$"
Relevant code on the follow up page:
<?php if(session_id() == ''){session_start();}
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
$woocommerce->show_messages();
?>
<div>session_id: <?php echo session_id(); ?><br />OUTPUT: <?php echo var_dump($_SESSION); ?></div>
<?php do_action( 'woocommerce_before_cart' ); ?>
<form action="<?php echo esc_url( $woocommerce->cart->get_cart_url() ); ?>" method="post">
Output of follow up page:
session_id: jl40cdmbdpd3h40qg3ogmghkp0
OUTPUT: array(2) { ["doc"]=> string(0) "" ["test"]=> string(18) "<>()!';!SDFSDFG^%$" }
EDIT: I wasn't viewing the raw HTML. /me shakes head.
IGNORE: One thing I found strange on the initial page is the single word "one" is showing up as a 32 char string. . . so there seems to be some extraneous white space after the document conversion. . . but it still outputs from the $_SESSION['doc'] variable on the initial screen.
Entire code for the initial page:
<?php if(session_id() == '' ){session_start();}
if(isset($_POST['added']))
{
add_basic_edit( $_POST['id'], $_POST['wc'] );
unset($_POST['added']);
header('Location: http://localhost/cart/');
exit;
}
/* Template Name: Upload Page */ get_header();
/**
* Test code to programmatically add a pricing calculator product to the cart, on
* every page load
*/
function add_basic_edit( $product_id, $num_words ) {
global $wc_measurement_price_calculator;
$product = get_product( $product_id ); // id of my 'word' product
$measurements = $wc_measurement_price_calculator->get_product_measurements( $product );
// get the one measurement
foreach ( $measurements as $measurement ) ;
$measurement->set_value( $num_words ); // the number of words
// add to cart
$wc_measurement_price_calculator->add_to_cart( $product->id, $measurement );
/var/www/html/websites/localhost/wordpress/wp-content/themes/Avada/upload.php}
if(isset($_POST['posted']))
{
require_once 'HTTP/Request2.php';
class DocumentConverterClient {
var $url = '';
function convert($inputFile, $outputType) {
$this->url="http://localhost:8080/jodconverter-sample-webapp-3.0-SNAPSHOT/converted/$inputFile.$outputType";
$request = new HTTP_Request2($this->url);
$request->setMethod(HTTP_Request2::METHOD_POST)
->setHeader('Content-Type', 'multipart/form-data')
->addPostParameter('outputFormat', $outputType)
->setBody($inputData);
$request->addUpload('inputDocument', $inputFile);
return $request->send()->getBody();
}
}
$documentConverter = new DocumentConverterClient();
$inputFile = $_FILES['inputDocument']['tmp_name'];
$outputType='txt';
$noext=current(explode(".", $_FILES["inputDocument"]["name"]));
$inputType=end(explode(".", $_FILES["inputDocument"]["name"]));
$outputFile = "data/$noext.$outputType";
$outputData = $documentConverter->convert($inputFile, $outputType);
//file_put_contents($outputFile, $outputData);
} ?>
<div class="column_group"><br /><br />
<div style="float:left;width:30%;" class="left_clumn">
<div style="float:left;width:35%">Wordcount:</div><div><?php $wc = str_word_count($outputData); echo $wc;?>
</div>
<div style="float:left;width:35%">Price Per Word:</div><div>$<?php $product_id=get_page_by_title('Basic Editing', 'OBJECT', 'Product' ); $product = get_product( $product_id->ID ); $ppw = $product->get_price(); echo $ppw;?>
</div>
<div style="float:left;width:35%">Total Price:</div><div>$<?php echo number_format(($wc * $ppw),2);?>
</div>
<div><br /><br />
<form name="add_to_cart" id="add_to_cart" action="../upload-page/" method="post">
<input type="hidden" name="added" value="added" />
<input type="hidden" name="id" value="<?php echo $product_id->ID; ?>" />
<input type="hidden" name="wc" value="<?php echo $wc; ?>" />
<?php $outputData = preg_replace("/.+/", "<p class='converted'>$0</p>", $outputData);
$outputData = preg_replace("/<\/p>\n\n/", "<br /><br /></p>", $outputData); ?>
<?php if(isset($outputData)){$_SESSION['doc'] = $outputData;}else{$_SESSION['doc']="JustATest";} $_SESSION['test'] = "<>()!';!SDFSDFG^%$"; echo "\n<div><br /><br />var_dump: " . var_dump($_SESSION) . "</div>"; ?>
<span class="BlueButton" onclick="document.getElementById('add_to_cart').submit()">Add to Cart</span>
</form>
</div>
</div>
</div>
<div style="float:right;width:70%;" class="right_column">
<div><?php echo $outputData; ?></div>
</div>
</div>
<div id="content" style="<?php echo $content_css; ?>">
</div>
<div id="sidebar" style="<?php echo $sidebar_css; ?>"><?php generated_dynamic_sidebar(); ?></div>
<?php get_footer(); unset($_POST['posted']); ?>
You must add session_start() to the top of all pages before anything is rendered as output to the page.
Source
This is a poor answer as I still don't know why $_SESSION['doc'] was getting overwritten, particularly since $_SESSION['test'] was retained. I think it has to do with my various if(isset()) statements for processing form data. I believe one of them was at some point overwritting $outputData. . . but I'm not certain where.
I changed the first such statement to grab outputData from the _POST variable:
if(isset($_POST['added']))
{
add_basic_edit( $_POST['id'], $_POST['wc'] );
unset($_POST['added']);
$_SESSION['doc'] = $_POST['outputData'];
header('Location: http://techleadnet.com/cart/');
exit;
}
I added a hidden input to the form, and a couple more regexes to get rid of single and double quotes:
$outputData = preg_replace("/\"/", """, $outputData);
$outputData = preg_replace("/\'/","’", $outputData);
<input type="hidden" name="outputData" value="<?php echo $outputData; ?>" />
If anybody can point out where I'm messing up either the $_SESSION['doc'] variable, or where $outputData is getting overwritten this would be a better answer.
I did try making session_start(), without an if statement, the first thing in both of these pages, but it didn't make a difference.
I have this code
<html>
<body background="http://newevolutiondesigns.com/images/freebies/abstract-background-22.jpg">
<br><br><br><br><br>
<DIV align="center">
<?php
$img = $_GET["img"];
?>
<?php
if($img=="") { echo ""; } else { echo "<img src='" . $img . "' />"; } ?>
<br>
</body></html>
I'm using this for screensnapr so when i'm trying to see the picture with http://imagesnappper.co.cc/Images?img= it autoamtically adds a / after ?img=
so it's like http://imagesnappper.co.cc/Images?img=/8kd6lx.jpg and it won't show the image how to delete that slash?
Try using...
$img = ltrim($img, "/");
There are a number of ways, but the simplest and nastiest that comes to mind is substr():
$img=substr($_GET[img], 1);
<?php
require 'db.php';
include_once("header.php");
include_once("functions.php");
include_once("profile.php");
if(isset($_POST['search_term'])){
$search_term = mysql_real_escape_string(htmlentities ($_POST['search_term']));
if(!empty($search_term)){
$search = mysql_query("SELECT `username`,`id` FROM `users` WHERE `username` LIKE '%$search_term%' and `business` <> 'business'");
$result_count = mysql_num_rows($search);
$suffix = ($result_count != 1) ? 's' : '';
echo '<div data-theme="a">Your search for <strong>' , $search_term ,'</strong> returned <strong>', $result_count,' </strong> record', $suffix, '</div>';
while($results_row = mysql_fetch_assoc($search)){
echo '<div data-theme="a"><strong>', "<img src='/image/<?php echo $image; ?>' width= 50px height=50px>", $results_row['username'], '</strong></div>';
$following = following($_SESSION['userid']);
if (in_array($key,$following)){
echo ' <div action= "action.php" method="GET" data-theme="a">
<input type="hidden" name="id" value="$key"/>
<input type="submit" name="do" value="follow" data-theme="a"/>
</div>';
}else{
echo " <div action='action.php' method='GET' data-theme='a'>
<input type='hidden' name='id' value='$key'/>
<input type='submit' name='do' value='follow' data-theme='a'/>
</div>";
}
}
}
}
?>
I would like some help putting the user image into the echo section of this code. I am not exactly sure how to do this so that it puts the image on the correct line of the search. Any advice would be greatly appreciated. Below is the line of code that I am referring too. Thanks.
while($results_row = mysql_fetch_assoc($search)) {
echo '',
"' width= 50px
height=50px>", $results_row['username'], '';
I don't see $image defined anywhere in your code, so I'm going to assume the image is being pulled from the database.
If that's the case then you'll want to do something like this:
while($results_row = mysql_fetch_assoc($search)){
echo '<div data-theme="a"><strong><img src="/image/'.$results_row['image'].'" style='width:50px;height:50px' />'.$results_row['username'].'</strong></div>';
}
just write:
..."<img src='/image/<?php echo $image; ?>' width='50' height='50'>", ...
but are you sure that 50x50 is suitable? you might want to set only width=50 and leave the browser to set height accordingly.
$image = 'someImage.png';
echo '<div data-theme="a"><strong>', "<img src='/image/{$image}' width= 50px height=50px>", $results_row['username'], '</strong></div>';
You would want something like this:
<?php
while($results_row = mysql_fetch_assoc($search)){
?>
<div data-theme="a">
<img src="/image/<?php echo $image; ?>" width="50px" height="50px">
<strong>
<?php echo $results_row['username']; ?>
</strong>
</div>
<?php
}
?>
A few things to check though:
dont use tags to wrap tags. They're generally used for text only
You're not selecting any image on your query.
don't use echo to output html its ugly.
don't use GET.
Use prepared statements (PDO or mysqli) to protect from mysql injection.