I have 2 separate questions. They are related so I hope it works to take care of both of them in one thread.

I have this code that works fine for what it does:

<head>
<script type="text/javascript">
function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

new_win = window.open('index.php?width=' + myWidth+'&height='+myHeight)}

</script>
</head>

<body>
<a href="javascript:alertSize();">Go to Album</a>
</body>

but I want to be automatically redirected without having to click the link "Go to album". Also I want the new page to open in the same tab, not a new one. I mostly copied and pasted the code from different sites so I'm not sure what all the code means. Is it the "new_win" that is telling it to open in a new window or tab and if so, what coding should be used to open in the same tab. Also, as I said I don't want to have to click anything.

Then:

On another page I want to somehow incorporate this script and combine the href variables with a variable submitted through php. Here is the code of the php file:

<head><link rel="stylesheet" href="../../../style.css" /></head>
<?php
// open this directory 
$myDirectory = opendir("./thumb");

// get each entry
while($entryName = readdir($myDirectory)) {
        $dirArray[] = $entryName;
}
// close directory
closedir($myDirectory);

//      count elements in array
$indexCount     = count($dirArray);
//Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
// loop through the array of thumnails and display them in table form
$width=$_REQUEST['width']*.8;

echo '<table align="center" width="80%">';
$index=2;
while($index < $indexCount) {
echo '<tr>';
$numcolumns=intval($width/120);
for($columns=$columns; $columns<$numcolumns; $columns++){
$cellwidth=intval(100/$numcolumns);
if($index>$indexCount-1){break;}
echo '<td width="'.$cellwidth.'%" align="center"><a href="view2.php?id='.$index.'"&height='.myHeight.'&width='.myWidth><img src="./thumb/'.$dirArray[$index].'" width="90%" alt="Smile"></td>';
$index++;
}
echo '</tr>';
$columns=0;
}
echo '</table>';
?>

In the red href code I want to also ad the myWidth and myHeight variables from the above javascript code (the green code is similar to what I want but I don't know that it is written correctly since they are javascript variables I am wanting but hopefully you get the idea of what I want). Since this is a different php file I will also need to add the javascript code in an echo statement but will probably need to modified somewhat since I will just be needing the variables from it and not open a new page. I hope you understand what I mean.

So could I get some help with the redirecting issue and also how I would need to change my javascript code and combine it with my php code in order to get the desired results.

Recommended Answers

All 2 Replies

First code: use window.location.href

<head>
<script type="text/javascript">
function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  window.location.href = 'index.php?width=' + myWidth+'&height='+myHeight;
 }

</script>
</head>

<body onLoad="alertSize();">
<a href="#">Go to Album</a>
</body>

Second Code: use $_GET to get url variable.

echo '<td width="'.$cellwidth.'%" align="center"><a href="view2.php?id='.$index.'"&height='.$_GET['myHeight'].'&width='.$_GET['myWidth'].'><img src="./thumb/'.$dirArray[$index].'" width="90%" alt="Smile"></td>';

Thanks a lot! That works great.

Now, I have another problem (challenge). I will first publish my code for another page and then explain my situation.

view2.php

<head><link rel="stylesheet" href="../../../style.css" /></head>
<?php
// open this directory 
$myDirectory = opendir("./thumb");

// get each entry
while($entryName = readdir($myDirectory)) {
        $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//      count elements in array
$indexCount     = count($dirArray);
//Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
        }
}
//$wdth=imagesx($photo);
//$hght=imagesy($photo);
$photo=$_REQUEST['id'];
//echo $photo;
//echo $indexCount;
$photoprev=$photo-1;
$photonext=$photo+1;

echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
echo '<html>';
echo '<head>';
echo '<title>'.$dirArray[$photo].'</title>';
echo '<body>';
echo '<table width="100%">';
echo '<tr>';
echo '<td width="2%">';
if($photoprev>1){
echo '<a href="view2.php?id='.$photoprev.'"><img src="previous.gif" height="80" width="40" alt="Smile"></a>';}
echo '</td>';
echo '<td width="96%" align="center"><a href="index.php"><img height="200%" src="'.$dirArray[$photo].'" alt="Smile"></a></td>';
echo '<td width="2%">';
if($photonext<$indexCount){
echo '<a href="view2.php?id='.$photonext.'"><img src="next.gif" height="80" width="40" alt="Smile"></a>';}
echo '</td>';
echo '</tr></table>';
echo '</body>';
echo '</html>';
?>

This is the php file used to view the photos. When I click the previous or next buttons it will reopen the page with a new id value in the href (shown in red). So in order to get the dimensions every time I reopen the same page I will need to resend the javascript code. Would you use the same javascript as in the First Code you explained and just ignore the window.location.href... and the <a href="#"...> but still keep the <body onLoad="alertSize();">?

The reason I would like to send the script every refresh is because of the fact that the user may change his/her browser size while viewing. I suppose I could just have a static value and they would then need to return to the index in order to refresh the dimension variables. If you have any other ideas about this they would be welcome but keep in mind I'm new to javascript and quite new to php so it will need to be kept simple.

Just out of curiosity is $_GET and $_REQUEST exactly the same and can they be interchanged.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.