generate multiple qr code in the same webpage - php

I need to generate mulitple qr code in the same webpage.
Which echo in a table,one row one qr code, and it is generated by calling echo 'img src = "generateqrcode.php"'.
Also I need to do some checking before printing each row the data and qr code.
However I found that the generated qr code were overwritten by the last one. Why this happened?
generateqrcode.php
session_start();
$key = $_SESSION['key'];
$link = TARGET_LINK.$key;
QRcode::png($link,false,"L",10,0);
A.php
$row=1;
while($row <=1){
echo '<table>';
echo '<td>';
echo $row;
echo '</td>';
echo '<td>';
$sql = GETSECTIONSQL;
if(!($qids = get_records_sql($query))){
$qids = array();
}
foreach($qids as $qid){
$qrsec = $qid->section;
if($section == $qrsec){
$sql2 = GETLINKSEQUENCESQL;
if(!($viewids = get_records_sql($query))){
$viewids = array();
}
foreach($viewids as $viewid){
$vid = $viewid->sequence;
session_start();
$_SESSION['vid'] = $vid;
echo '<td>';
echo '<a class="fancybox" href="generateqrcode.php">';
echo '<img src="generateqrcode.php"/></a>';
echo '</td>';
}
}
}
echo '</table>';
$row++;
}
New
A.php
foreach($viewids as $viewid){
$vid = $viewid->sequence;
echo '<td>';
echo '<a class="fancybox" href="generateqrcode.php">';
echo '<img src="generateqrcode.php?id=$vid"/></a>';
echo '</td>';
}
generateqrcode.php
$id = $_GET['id'];
$link = TARGET_LINK.$id;
QRcode::png($link,false,"L",10,0);

Well, all your images are linking to the exact same "image" generateqrcode.php. Of course they're all going to look the same. Since your session can only store one value at a time, you cannot transport any ids unique per image in the session. You should make unique links to unique images:
<img src="generateqrcode.php?id=1234567">
Then use $_GET['id'] when generating the image instead of the session value.

Related

Change table row styleclass under certain mysql conditions

I have the following table created with a foreach loop.
**foreach($data2['wow_accounts']['0']['characters'] as $key => $item) {
echo '<tr>';
echo '<td>';
echo $item['name'];
echo '</td>';
echo '<td>';
echo $item['realm']['name'];
echo '</td>';
echo '<td>';
echo '<button class="btnSelect">Select</button>';
echo '</td>';
echo '</tr>';
}
echo '</table>';**
I want that the script checks inside of the table "y4qt2_jsn_users" of the mysql database and under a certain id number, if the "params" entry is "ja". If this is true then this certain table row should get another tr class.
My idea is something like that, but how can i combine this with my foreach table loop?
$result = mysqli_query("SELECT params FROM xxx_users WHERE params = 'ja' AND id= '$id'");
if(mysqli_num_rows($result) == 0) {
// row not found, just loop without a certain <tr class=""
} else {
// row found, give this row a <tr class=""
}
Here is a screenshot of what I want. If the mysql condition "params=ja" is true the whole row should get a new tr styleclass, not just a cell.
Screenshot
Screenshot-Database
Assuming I understand correctly, this should work. Please sanitize database queries (especially if there is user input at any point). But as mentioned in the comments, I don't know where $id is set, and actually presume it's a value in the $item array, so maybe replaced with $item['Id']
foreach($data2['wow_accounts']['0']['characters'] as $key => $item) {
$result = mysql_query("SELECT params FROM y4qt2_jsn_users WHERE params = 'ja' AND
id= '$id'");
if(mysql_num_rows($result) == 0) {
$class_string = '';
} else {
$class_string = ' class="my-additional-class"';
}
echo '<tr'.$class_string.'>';
echo '<td>';
echo $item['name'];
echo '</td>';
echo '<td>';
echo $item['realm']['name'];
echo '</td>';
echo '<td>';
echo '<button class="btnSelect">Select</button>';
echo '</td>';
echo '</tr>';
}
There are a lot of ways to clean this up, and if you wanted fewer lines of code you can replace the entire if else block and the $class_string variable by using ternary operators inline. The goal was to make it easy for you to read and simple to understand. (I also prefer very verbose code myself)

Auto increment through a php loop

I have a case where I am using the code below to populate a template of sorts. Within the templates' javascript I would individually check the data attribute field I setup, essentially causing me to have multiple JS files instead of one that is shared. I then thought I could use a generic name field too, but prepend a number through the loop.
For example, with the line of code below where the `name="testField". I want to see if there is a way that I can add a number, but auto increment it through the loop with php.
Is this possible?
echo '<div class="markerItem" name="testField' . $number . '" "data-marker="' . $marker_data . '">';
PHP Code
if ($marker_stmt = $con->prepare($sql_marker)) {
$marker_stmt->execute();
$marker_rows = $marker_stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<div id="projMarker">';
foreach ($marker_rows as $marker_row) {
$marker_solution = $marker_row['solution'];
$maker_item = $marker_row['subSolution'];
$marker_data = $marker_row['subSolution'];
echo '<div class="markerItem" data-marker="' . $marker_data . '">';
echo $marker_item;
echo '</div>';
}
}
echo '</div>';
if ($marker_stmt = $con->prepare($sql_marker)) {
$marker_stmt->execute();
$marker_rows = $marker_stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<div id="projMarker">';
foreach ($marker_rows as $key=>$marker_row) {
$marker_solution = $marker_row['solution'];
$maker_item = $marker_row['subSolution'];
$marker_data = $marker_row['subSolution'];
echo '<div class="markerItem" name="testField_'.$key.'" data-marker="' . $marker_data . '">';
echo $marker_item;
echo '</div>';
}
}
echo '</div>';
Using this, $key is assigned from the array index which will be a number starting from 0 and ending at count($marker_rows)-1.
Suprisingly, I couldn't find an appropriate duplicate for this.
You can easily increment or decrement variables in PHP.
$number = 0;
foreach ($marker_rows as $marker_row) {
...
$number++; // $number will now be $number+1
}
You can use $number++ directly in your attribute (if concatenating) as this will return the current $number then increment the value.

php while loop database data

the below while loop statement loops mysqli query result(text and image from database). A row can contain text only or image and text. I'm trying to display text only if the image field is empty and display both image and text when they both exist in a row. However, i tried using the if statement in the while loop statement below, the problem is that when it suppose to display only text,it displays text with image tag with no src value,kindly help fix this.
Thanks.
while($row = mysqli_fetch_assoc($query)){
$id = $row["id"];
$text = $row["texts"];
$image =$row['images'];
if(!empty($text) && !empty($image)){
echo '<img src='.$image.'/>';
echo '<div>'.$text.'</div>';
}elseif( !empty($text) && empty($image) ){
echo '<div>'.$text.'</div>';
}
}
$row['images'] might contain spaces, try trimming it. Also, you can simplify the if:
while($row = mysqli_fetch_assoc($query)){
$id = $row["id"];
$text = trim($row["texts"]);
$image = trim($row['images']);
if(!empty($image)){
echo '<img src='.$image.'/>';
}
if(!empty($text)){
echo '<div>'.$text.'</div>';
}
}
while($row = mysqli_fetch_assoc($query)){
$id = $row["id"];
$text = $row["texts"];
$image =$row['images'];
if($image)
echo "<img src='{$image}' />";
if($text)
echo "<div>{$text}</div>";
}

implementing next and back buttons for a slideshow

I'm trying to make a php slideshow and I'm almost done I just need to implement the next and back buttons which I thought were going to be easy, but apparently you can't increment indexes in php?
$sql = "SELECT pic_url FROM pic_info";
$result = $conn->query($sql);
$count = 0;
$dir = "http://dev2.matrix.msu.edu/~matrix.training/Holmberg_Dane/";
$source = "gallery.php";
if ($result->num_rows > 0) {
// output data of each row
$pic_array = array();
while ($row = $result->fetch_assoc()) {
$pic_array[$count] = $row['pic_url'];
$count++;
}
$index = 1;
echo "<img src= ' $dir$pic_array[$index]' />";
echo "<a href= '$dir$pic_array[$index + 1]'>next</a>";
echo "<a href= '$dir$pic_array[$index - 1]'>back</a>";
}
$conn->close();
?>
Try this
<?php
echo '<img src="'.$dir.$pic_array[$index].'">
next
back';
?>
I would suggest to place the url's in an array and loop over them.
$urls = ['website/url/for/image/image.jpg', 'another/url/image.jpg'];
foreach ($urls as $url) {
echo 'a href="http://'.$url.'"> Click </a>';
}
It is definitely more readable that way.

Assign image to PHP variable

I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}
Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred
$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}
$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}
Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.
<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>
please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>

Categories