How would I add the data from this query to an array? - php

Is there a way that I can put the data from the while loop into an array and output it using echo? I've tried defining an array and just appending it with .= but it does not add.
$query = "SELECT * FROM coils WHERE name like '%$term%' or
resistance like '%$term%' or
wraps like '%$term%' or
wire_one like '%$term%' or
wire_two like '%$term%' or
wire_three like '%$term%' or
wire_four like '%$term%' or
wire_five like '%$term%' or
wire_six like '%$term%'
LIMIT 25";
$prep = $db->getConnection()->prepare($query);
$result = $prep->execute();
$rowCount = $prep->rowCount();
if ($rowCount <= 0) {
echo "<script>alert('No Results, please try another search');</script>";
}
while($row = $prep->fetch(PDO::FETCH_ASSOC)) {
echo "<a href='coil.php?id=" . $row['uniqueid'] . "'>";
echo "<div id='search_result'>";
echo "<div id='search_title'>Name: " . $row['name'] . "</div>";
echo "<div id='search_ohms'>Resistance: " . $row['resistance'] . "</div>";
echo "<div id='search_wraps'>Wraps: " . $row['wraps'] . "</div>";
echo "<div id='search_around'>Wrapped Around: " . $row['wrapped'] . "</div>";
echo "<div id='search_description'>" . $row['description'] . "</div>";
echo "</div>";
echo "</a>";
}

Try to add output in an array and then use implode().
$temp = array();
while($row = $prep->fetch(PDO::FETCH_ASSOC)) {
$temp[] = "<a href='coil.php?id=" . $row['uniqueid'] . "'>";
$temp[] = "<div id='search_result'>";
$temp[] = "<div id='search_title'>Name: " . $row['name'] . "</div>";
$temp[] = "<div id='search_ohms'>Resistance: " . $row['resistance'] . "</div>";
$temp[] = "<div id='search_wraps'>Wraps: " . $row['wraps'] . "</div>";
$temp[] = "<div id='search_around'>Wrapped Around: " . $row['wrapped'] . "</div>";
$temp[] = "<div id='search_description'>" . $row['description'] . "</div>";
$temp[] = "</div>";
$temp[] = "</a>";
}
echo implode(' ', $temp); //with or without space

You can get all the data in an array with:
$data = $prep->fetchAll(PDO::FETCH_ASSOC);
To display it, you can close PHP with ?>, then use the alternate syntax for nice clean HTML:
<?php foreach ($data as $row) : ?>
<a href="coil.php?id=<?= htmlentities($row['uniqueid']) ?>">
<div id='search_result'>
<div id='search_title'>Name: <?= htmlentities($row['name']) ?></div>
<div id='search_ohms'>Resistance: <?= htmlentities($row['resistance']) ?></div>
<div id='search_wraps'>Wraps: <?= htmlentities($row['wraps']) ?></div>
<div id='search_around'>Wrapped Around: <?=htmlentities($row['wrapped']) ?></div>
<div id='search_description'><?= htmlentities($row['description']) ?></div>
</div>
</a>
<?php endforeach ?>

Related

How to get a post in php where use sql like that :

How do i get the row id?
I'm doing a site of music, i need do the admin page,this part is when the admin gonna edit the albums. I need the id to specify what music gonna change the informations.
<?php
$db = mysqli_connect("localhost", "root", "", "datamusic");
$result = mysqli_query($db, "SELECT * FROM musics");
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='images/" . $row['Image'] . "' >";
echo "<p>" . $row['Band/Singer'] . "</p>";
echo "<form method='POST' action='editInformations.php'><p name=" . $row['id'] . ">ID = " . $row['id'] . "</p></form>";
echo "<a href='editInformations.php'><button type='submit'>Editar</button></a>";
echo "</div>";
}
?>
I try something like that, but doesn't works
//editInformations.php
$id =$_POST[$row['id']];
echo $id;
Get edit page GET funciton. You can put id for edit.
editInformations.php?id=".$row['id']."
Change code with this.
<?php
$db = mysqli_connect("localhost", "root", "", "datamusic");
$result = mysqli_query($db, "SELECT * FROM musics");
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='images/" . $row['Image'] . "' >";
echo "<p>" . $row['Band/Singer'] . "</p>";
echo "<form method='POST' action='editInformations.php'><p name=" . $row['id'] . ">ID = " . $row['id'] . "</p></form>";
echo "<a href='editInformations.php?id=".$row['id']."'><button type='submit'>Editar</button></a>";
echo "</div>";
}
?>
//editInformations.php
$id =$_GET['id'];
echo $id;

SQL query not returning array

Working on getting a page to build off of an array that is returned from a DB to post a story, not sure what it is not working. The page URL looks like this: https://ohcrap.ninja/games/ps4/article.php?id=1
Here is the code that should be generating the content:
<?php
$id = $_GET['id'];
$query = mysqli_query($con,'SELECT * FROM `PS4` WHERE `id` =' .$id) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($query));
// Echo page content
echo "<div class='col s12 m12 l12'>";
echo "<div class='card small grey darken-3'>";
echo "<div class='card-stacked'>";
echo "<div class='card-content'>";
echo "$id";
echo "<span class='card-title'>" . $row['title'] . "</span>";
echo "<hr color='black'>";
echo "<P>By:<i> " . $row['author'] . "</i></P>";
echo "<P>Published: " . $row['published'] . "</P>";
echo "<br>";
echo "<P class='truncate'>" . $row['story'] . "</P>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
Your while loop is not doing anything useful, because you're immediately ending it with that ;.
while ($row = mysqli_fetch_array($query)) {
// all those echoes
}

jquery simplecart php variables

I'm having difficulties with the onclick jquery within my php code, how do I get a php variable so that name=variable and price=variable?
<?php
$result = mysqli_query($db,"SELECT * FROM table");
while($row = mysqli_fetch_array($result)) {
echo '<ul>';
echo '<li>' . $row['selection'] . '<div style="float:right;"><a href="#" onclick="simpleCart.add('name='. $row['selection'] . ','price='. $row['price'] .');return false;
"> '. $row['price'] . '</a></div></li>';
echo '</ul>';
}
?>
How do I get around this? Thanks
change your code with this line...
echo '<div style="float:right;"><a href="#"
onclick="simpleCart.add(name='.$row['selection'].',price='.$row['price'].')"
</a></div>';
Not faniliar with simplecart, but You definitely need to crrectly escape your quotes.
<?php
$result = mysqli_query($db, "SELECT * FROM table");
while($row = mysqli_fetch_array($result)) {
echo '<ul>';
echo '<li>' . $row['selection'] . '<div style="float:right;"> '. $row['price'] . '</div></li>'; echo '</ul>'; }
?>

Merge values into a while loop

below I have values and below that I have a while loop with multiple values that are sorted by $top_list_friends['workouts'], with the highest number at top.
How can a merge the below values into one list?
echo "<div class='user'>";
echo "<span class='number'>" . $my_rank . "</span>";
echo "<span class='name'>" . $personal['name'] . "</span>";
echo "<span class='workouts'>" . $personal['workouts'] . "</span>";
echo "</div>";
$i = 0;
while ($top_list_friends = mysql_fetch_array($query_top_list_friends)) {
$i++;
echo "<div class='user'>";
echo "<span class='number'>" . $i . "</span>";
echo "<span class='name'>" . $top_list_friends['name'] . "</span>";
echo "<span class='workouts'>" . $top_list_friends['workouts'] . "</span>";
echo "</div>";
}
Didn't understand question very well, but I would do it in this way:
$name = $personal['name'];
$workouts = $personal['workouts'];
$html = <<<html
<div class='user'>
<span class='number'>$my_rank</span>
<span class='name'>$name</span>
<span class='workouts'>$workouts</span>
</div>
html;
$i=0;
while ($top_list_friends = mysql_fetch_array($query_top_list_friends)) {
$i++;
$topName = top_list_friends['name'];
$topW = $top_list_friends['workouts']
$html += <<<html
<div class='user'>
<span class='number'>$i</span>
<span class='name'>$topName</span>
<span class='workouts'>$topW</span>
</div>
html;
}
echo $html;
At the beginning:
$inserted=0;
into the loop:
if(!$inserted && $personal['workouts']> $top_list_friends['workouts'])}
$inserted=0;
//first div echoes
}
at the end
if(!$inserted){
//the same echos
}

Is there a better or more efficient way to do this?

This code is working for what I need it to do, but (in my opinion) it looks bad so I am hoping someone knows of a cleaner or more efficient way to do the same thing. I have several entries being pulled from the database and I want them to be styled identically. Only the logo and the the link name will change eventually I will add a description. Here is the code:
<div class="content">
<?PHP
while($row = $stmt->fetch())
{
$name = $row['name'];
$id = $row['id'];
$logo = $row['logo'];
$username = $row['username'];
echo "<div class=" . "Links" . ">";
echo "<div class=" . "linkImages" . ">";
echo "<br>" . "" . "<img src=" . "users/" . $username . "/images/" . $logo . " " . "width=" . "200" . " " . "height=" . "auto" . " " . "border=" . "0" . "/>" . "";
echo "</div>";
echo "<div class=" . "linkName" . ">";
echo "" . $name ."";
echo "</div>";
echo "</div>";
}
?>
</div>
You can trivially remove most of the echoes and string concatenation by switching to a HEREDOC:
while($row = $stmt->fetch()) {
echo <<<EOL
<div class="links">
yadayada
<br><a href="Profile.php?id={$row['id']}"><img src="users/{$row['username']}" etc....
yada yada yada
EOL;
Note that the lack of escapes in there, allowing for proper quotes around the tag attributes, and the {} notation on the embedded variables.
Don't use extra variable names. Instead, use the original.
Also, don't output every row with PHP. Use plain HTML and add the variable in it later:
<div class="Links">
<?=$row['name']?>
Or just echo as 1 line, no need for concatenation
echo "<div class=\"linkImages\">";
or
echo '<div class="linkImages">';
echo '<div class="content">';
while($row = $stmt->fetch()){
$name = $row['name'];
$id = $row['id'];
$logo = $row['logo'];
$username = $row['username'];
echo '<div class="Links">
<div class="linkImages">
<br><img src="users/'.$username.'/images/'. $logo .'" width="200" height="auto" border="0">
</div>
<div class="linkName">
'.$name.'
</div>
</div>';
}
echo '</div>';
Here's how I would write it:
<div class="content">
<?php
while ($row = $stmt->fetch()){
echo '<div class="Links">';
echo '<div class="linkImages">';
echo '<br /><img src="users/'. $row['username'] .'/images/'. $row['logo'] .'" width="200" />';
echo '</div>';
echo '<div class="linkName">';
echo ''. $row['name'] .'';
echo '</div>';
}
?>
</div>
Note that I removed the border="0" for the img tag - that should be done with CSS.
The short answer is yes. There is almost always a cleaner or more efficient way to do it.
How about something like this?
<div class="content">
<?PHP while($row = $stmt->fetch()) { ?>
<div class="Links">
<div class="linkImages">
<br><img src="users/<?=$row['username'] ?>/images/<?=$row['logo'] ?> width="200" height="auto" border="0" />
</div>
<div class="linkName">
<a href="Profile.php?id="<?=$row['id'] ?>><?=$row['name'] ?></a>
</div>
</div>
<?PHP } ?>
</div>
There are a lot of erroneous symbols etc in this, it comes with practice, but something like this might be worth trying
<?php
while($row = $stmt->fetch()) {
$string = "";
$string .= "<div class=\"Links\">\n";
$string .= "<div class=\"linkImages\">\n";
$string .= "<br />\n";
$string .= "<img src=\"users/". $row['name'] ."/images/" . $row['logo'] . "\" width=\"200\" height=\"auto\" border=\"0\" />\n";
$string .= "</div>\n";
$string .= "<div class=\"linkName\">\n";
$string .= "". $row['name'] ."\n";
$string .= "</div>\n";
$string .= "</div>\n";
echo $string;
}
?>
I'd recommend learning how to use the printf() family of functions.
$frame = '<img src="users/%s/images/%s" width="200" height="auto" border="0"/>';
printf($frame, $id, $username, $logo);

Categories