I'm writing a web page which generates a list of Blog Posts, along with associated data (such as timestamps and previews of the articles), and I'm attempting to generate a unique variable for every blogpost generation that increments by one.
In order to do this, I've created a variable called "name" on line 59, and this would take the string "dtartnum" and concatenate it with an integer i of the for loop, with a maximum of i, or the variable $sqlindex (which is the index of the number of articles in the database).
On line 60, I have a variable I want to define with the result of the variable name, and assign it the value of the $sqlindex (with result_of_name just being a placeholder for the result of name each time it's defined). The reason for this (based on other articles on Stack Overflow that I've read) is that PHP only executes during the page load (roughly).
In this line of logic, that means that the PHP variables would become inaccessible, and would have to be procedural generated and stored locally as PHP generates the page.
Later on, I'm planning on using an AJAX library I've found to Post the variable to a page (which has yet to be created) that would act as the "id" (which is also the PRIMARY KEY) for the rows in a MySQL database I have (so I could recall the row and associated information for the page). On the other page, I'd have a template and a Fill-In-The-Blank MySQL query waiting for the "id" of the row from the page I've pasted above.
Looking through Stack Overflow, I've found something similar to this in Bash, however, seeing as the languages are so syntactically and functionally different, I couldn't find much of an application for this in JS. I could not find something like this for anything closer to JS.
So, in summary, my question is this:
How do I define the variable under "name" with the value of the variable name? i.e. if name = "potato"; then the variable under would be potato = $sqlindex;
and, I guess in extension
Is there a better way to go about this? Is there something structurally that could be improved? Please let me know so I can use this for future projects.
The Code in question is towards the bottom of the HEREDOC, above the commented-out block.
If you need me to add anything, let me know.
The Code:
<?php
$conn = new mysqli($servername, $username, $password, $db);
//check Connection
if ($conn->connect_error){
die("ERROR: ". $conn->connect_error);
}
else{
//nothing, essentially
}
$rowcountsql= "SELECT id FROM writeups ORDER BY id;";
if ($result=$conn->query($rowcountsql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
}
for ($sqlindex = 0; $sqlindex <= $rowcount; $sqlindex++){
if ($result = $conn->query("SELECT * FROM writeups LIMIT ".$sqlindex.", 1;")){
if ($count = $result->num_rows){
// echo "The row count for this query is ",$count,"<br>";
while($row = $result->fetch_object()){
$titlecontent = $row->title;
$writeupcontent = $row->body;
$rowdateandtime = $row->dateandtime;
$article_number = $sqlindex + 1;
echo $str=<<<ARTICLE
<div class="article">
<div class="titleanddate">
<h2>$titlecontent</h2>
<h3 class="dtartnum">Written $rowdateandtime, Article Number $article_number</h3>
</div>
<div class="truncate">
<p class="writeup">
$writeupcontent
</p>
<a href="templatepage.php" onmouseenter="assignCookie()">Read More...</a>
<script>
function assignCookie(){
//total nr in list
window.alert(document.getElementsByClassName("article").length);
//pos in list and definition of variable per button
for (i=0; i<=sqlindex;i++){
var name = "dtartnum"+i;
var result_of_name = $sqlindex;
window.alert(result_of_name);//how do we get this to work?
}
/* This code will be developed later, I suppose... need to fix the above first
window.alert(dtartnum);
var pattern = /Article Number \d{1,3}/;
if(pattern.test(document.getElementsByClassName(dtartnum).value)){
window.alert(pattern.test(document.getElementsByClassName(dtartnum).value));
}
else{
window.alert(getElementsByClassName(dtartnum).value);
}*/
}
</script>
</div>
</div>
ARTICLE;
}
}
}
}
?>